ADDIP failure
[sahlberg/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 "lib/tevent/tevent.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/network.h"
26 #include "system/locale.h"
27 #include "popt.h"
28 #include "cmdline.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_client.h"
31 #include "../include/ctdb_private.h"
32 #include "../common/rb_tree.h"
33 #include "db_wrap.h"
34
35 #define ERR_TIMEOUT     20      /* timed out trying to reach node */
36 #define ERR_NONODE      21      /* node does not exist */
37 #define ERR_DISNODE     22      /* node is disconnected */
38
39 struct ctdb_connection *ctdb_connection;
40
41 static void usage(void);
42
43 static struct {
44         int timelimit;
45         uint32_t pnn;
46         int machinereadable;
47         int verbose;
48         int maxruntime;
49 } options;
50
51 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
52 #define LONGTIMELIMIT() timeval_current_ofs(options.timelimit*10, 0)
53
54 #ifdef CTDB_VERS
55 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
56 {
57 #define STR(x) #x
58 #define XSTR(x) STR(x)
59         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
60         return 0;
61 }
62 #endif
63
64
65 /*
66   verify that a node exists and is reachable
67  */
68 static void verify_node(struct ctdb_context *ctdb)
69 {
70         int ret;
71         struct ctdb_node_map *nodemap=NULL;
72
73         if (options.pnn == CTDB_CURRENT_NODE) {
74                 return;
75         }
76         if (options.pnn == CTDB_BROADCAST_ALL) {
77                 return;
78         }
79
80         /* verify the node exists */
81         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
82                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
83                 exit(10);
84         }
85         if (options.pnn >= nodemap->num) {
86                 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
87                 exit(ERR_NONODE);
88         }
89         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DELETED) {
90                 DEBUG(DEBUG_ERR, ("Node %u is DELETED\n", options.pnn));
91                 exit(ERR_DISNODE);
92         }
93         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
94                 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
95                 exit(ERR_DISNODE);
96         }
97
98         /* verify we can access the node */
99         ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
100         if (ret == -1) {
101                 DEBUG(DEBUG_ERR,("Can not access node. Node is not operational.\n"));
102                 exit(10);
103         }
104 }
105
106 /*
107  check if a database exists
108 */
109 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
110 {
111         int i, ret;
112         struct ctdb_dbid_map *dbmap=NULL;
113
114         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
115         if (ret != 0) {
116                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
117                 return -1;
118         }
119
120         for(i=0;i<dbmap->num;i++){
121                 const char *name;
122
123                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
124                 if (!strcmp(name, db_name)) {
125                         return 0;
126                 }
127         }
128
129         return -1;
130 }
131
132 /*
133   see if a process exists
134  */
135 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
136 {
137         uint32_t pnn, pid;
138         int ret;
139         if (argc < 1) {
140                 usage();
141         }
142
143         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
144                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
145                 return -1;
146         }
147
148         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
149         if (ret == 0) {
150                 printf("%u:%u exists\n", pnn, pid);
151         } else {
152                 printf("%u:%u does not exist\n", pnn, pid);
153         }
154         return ret;
155 }
156
157 /*
158   display statistics structure
159  */
160 static void show_statistics(struct ctdb_statistics *s, int show_header)
161 {
162         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
163         int i;
164         const char *prefix=NULL;
165         int preflen=0;
166         int tmp, days, hours, minutes, seconds;
167         const struct {
168                 const char *name;
169                 uint32_t offset;
170         } fields[] = {
171 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
172                 STATISTICS_FIELD(num_clients),
173                 STATISTICS_FIELD(frozen),
174                 STATISTICS_FIELD(recovering),
175                 STATISTICS_FIELD(num_recoveries),
176                 STATISTICS_FIELD(client_packets_sent),
177                 STATISTICS_FIELD(client_packets_recv),
178                 STATISTICS_FIELD(node_packets_sent),
179                 STATISTICS_FIELD(node_packets_recv),
180                 STATISTICS_FIELD(keepalive_packets_sent),
181                 STATISTICS_FIELD(keepalive_packets_recv),
182                 STATISTICS_FIELD(node.req_call),
183                 STATISTICS_FIELD(node.reply_call),
184                 STATISTICS_FIELD(node.req_dmaster),
185                 STATISTICS_FIELD(node.reply_dmaster),
186                 STATISTICS_FIELD(node.reply_error),
187                 STATISTICS_FIELD(node.req_message),
188                 STATISTICS_FIELD(node.req_control),
189                 STATISTICS_FIELD(node.reply_control),
190                 STATISTICS_FIELD(client.req_call),
191                 STATISTICS_FIELD(client.req_message),
192                 STATISTICS_FIELD(client.req_control),
193                 STATISTICS_FIELD(timeouts.call),
194                 STATISTICS_FIELD(timeouts.control),
195                 STATISTICS_FIELD(timeouts.traverse),
196                 STATISTICS_FIELD(total_calls),
197                 STATISTICS_FIELD(pending_calls),
198                 STATISTICS_FIELD(lockwait_calls),
199                 STATISTICS_FIELD(pending_lockwait_calls),
200                 STATISTICS_FIELD(childwrite_calls),
201                 STATISTICS_FIELD(pending_childwrite_calls),
202                 STATISTICS_FIELD(memory_used),
203                 STATISTICS_FIELD(max_hop_count),
204         };
205         tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
206         seconds = tmp%60;
207         tmp    /= 60;
208         minutes = tmp%60;
209         tmp    /= 60;
210         hours   = tmp%24;
211         tmp    /= 24;
212         days    = tmp;
213
214         if (options.machinereadable){
215                 if (show_header) {
216                         printf("CTDB version:");
217                         printf("Current time of statistics:");
218                         printf("Statistics collected since:");
219                         for (i=0;i<ARRAY_SIZE(fields);i++) {
220                                 printf("%s:", fields[i].name);
221                         }
222                         printf("num_reclock_ctdbd_latency:");
223                         printf("min_reclock_ctdbd_latency:");
224                         printf("avg_reclock_ctdbd_latency:");
225                         printf("max_reclock_ctdbd_latency:");
226
227                         printf("num_reclock_recd_latency:");
228                         printf("min_reclock_recd_latency:");
229                         printf("avg_reclock_recd_latency:");
230                         printf("max_reclock_recd_latency:");
231
232                         printf("num_call_latency:");
233                         printf("min_call_latency:");
234                         printf("avg_call_latency:");
235                         printf("max_call_latency:");
236
237                         printf("num_lockwait_latency:");
238                         printf("min_lockwait_latency:");
239                         printf("avg_lockwait_latency:");
240                         printf("max_lockwait_latency:");
241
242                         printf("num_childwrite_latency:");
243                         printf("min_childwrite_latency:");
244                         printf("avg_childwrite_latency:");
245                         printf("max_childwrite_latency:");
246                         printf("\n");
247                 }
248                 printf("%d:", CTDB_VERSION);
249                 printf("%d:", (int)s->statistics_current_time.tv_sec);
250                 printf("%d:", (int)s->statistics_start_time.tv_sec);
251                 for (i=0;i<ARRAY_SIZE(fields);i++) {
252                         printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
253                 }
254                 printf("%d:", s->reclock.ctdbd.num);
255                 printf("%.6f:", s->reclock.ctdbd.min);
256                 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
257                 printf("%.6f:", s->reclock.ctdbd.max);
258
259                 printf("%d:", s->reclock.recd.num);
260                 printf("%.6f:", s->reclock.recd.min);
261                 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
262                 printf("%.6f:", s->reclock.recd.max);
263
264                 printf("%d:", s->call_latency.num);
265                 printf("%.6f:", s->call_latency.min);
266                 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
267                 printf("%.6f:", s->call_latency.max);
268
269                 printf("%d:", s->lockwait_latency.num);
270                 printf("%.6f:", s->lockwait_latency.min);
271                 printf("%.6f:", s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0);
272                 printf("%.6f:", s->lockwait_latency.max);
273
274                 printf("%d:", s->childwrite_latency.num);
275                 printf("%.6f:", s->childwrite_latency.min);
276                 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
277                 printf("%.6f:", s->childwrite_latency.max);
278                 printf("\n");
279         } else {
280                 printf("CTDB version %u\n", CTDB_VERSION);
281                 printf("Current time of statistics  :                %s", ctime(&s->statistics_current_time.tv_sec));
282                 printf("Statistics collected since  : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
283
284                 for (i=0;i<ARRAY_SIZE(fields);i++) {
285                         if (strchr(fields[i].name, '.')) {
286                                 preflen = strcspn(fields[i].name, ".")+1;
287                                 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
288                                         prefix = fields[i].name;
289                                         printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
290                                 }
291                         } else {
292                                 preflen = 0;
293                         }
294                         printf(" %*s%-22s%*s%10u\n", 
295                                preflen?4:0, "",
296                                fields[i].name+preflen, 
297                                preflen?0:4, "",
298                                *(uint32_t *)(fields[i].offset+(uint8_t *)s));
299                 }
300                 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);
301
302                 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);
303
304                 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);
305                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "lockwait_latency   MIN/AVG/MAX", s->lockwait_latency.min, s->lockwait_latency.num?s->lockwait_latency.total/s->lockwait_latency.num:0.0, s->lockwait_latency.max, s->lockwait_latency.num);
306                 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);
307         }
308
309         talloc_free(tmp_ctx);
310 }
311
312 /*
313   display remote ctdb statistics combined from all nodes
314  */
315 static int control_statistics_all(struct ctdb_context *ctdb)
316 {
317         int ret, i;
318         struct ctdb_statistics statistics;
319         uint32_t *nodes;
320         uint32_t num_nodes;
321
322         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
323         CTDB_NO_MEMORY(ctdb, nodes);
324         
325         ZERO_STRUCT(statistics);
326
327         for (i=0;i<num_nodes;i++) {
328                 struct ctdb_statistics s1;
329                 int j;
330                 uint32_t *v1 = (uint32_t *)&s1;
331                 uint32_t *v2 = (uint32_t *)&statistics;
332                 uint32_t num_ints = 
333                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
334                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
335                 if (ret != 0) {
336                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
337                         return ret;
338                 }
339                 for (j=0;j<num_ints;j++) {
340                         v2[j] += v1[j];
341                 }
342                 statistics.max_hop_count = 
343                         MAX(statistics.max_hop_count, s1.max_hop_count);
344                 statistics.call_latency.max = 
345                         MAX(statistics.call_latency.max, s1.call_latency.max);
346                 statistics.lockwait_latency.max = 
347                         MAX(statistics.lockwait_latency.max, s1.lockwait_latency.max);
348         }
349         talloc_free(nodes);
350         printf("Gathered statistics for %u nodes\n", num_nodes);
351         show_statistics(&statistics, 1);
352         return 0;
353 }
354
355 /*
356   display remote ctdb statistics
357  */
358 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
359 {
360         int ret;
361         struct ctdb_statistics statistics;
362
363         if (options.pnn == CTDB_BROADCAST_ALL) {
364                 return control_statistics_all(ctdb);
365         }
366
367         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
368         if (ret != 0) {
369                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
370                 return ret;
371         }
372         show_statistics(&statistics, 1);
373         return 0;
374 }
375
376
377 /*
378   reset remote ctdb statistics
379  */
380 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
381 {
382         int ret;
383
384         ret = ctdb_statistics_reset(ctdb, options.pnn);
385         if (ret != 0) {
386                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
387                 return ret;
388         }
389         return 0;
390 }
391
392
393 /*
394   display remote ctdb rolling statistics
395  */
396 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
397 {
398         int ret;
399         struct ctdb_statistics_wire *stats;
400         int i, num_records = -1;
401
402         if (argc ==1) {
403                 num_records = atoi(argv[0]) - 1;
404         }
405
406         ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
407         if (ret != 0) {
408                 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
409                 return ret;
410         }
411         for (i=0;i<stats->num;i++) {
412                 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
413                         continue;
414                 }
415                 show_statistics(&stats->stats[i], i==0);
416                 if (i == num_records) {
417                         break;
418                 }
419         }
420         return 0;
421 }
422
423
424 /*
425   display uptime of remote node
426  */
427 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
428 {
429         int ret;
430         struct ctdb_uptime *uptime = NULL;
431         int tmp, days, hours, minutes, seconds;
432
433         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
434         if (ret != 0) {
435                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
436                 return ret;
437         }
438
439         if (options.machinereadable){
440                 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
441                 printf(":%u:%u:%u:%lf\n",
442                         (unsigned int)uptime->current_time.tv_sec,
443                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
444                         (unsigned int)uptime->last_recovery_finished.tv_sec,
445                         timeval_delta(&uptime->last_recovery_finished,
446                                       &uptime->last_recovery_started)
447                 );
448                 return 0;
449         }
450
451         printf("Current time of node          :                %s", ctime(&uptime->current_time.tv_sec));
452
453         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
454         seconds = tmp%60;
455         tmp    /= 60;
456         minutes = tmp%60;
457         tmp    /= 60;
458         hours   = tmp%24;
459         tmp    /= 24;
460         days    = tmp;
461         printf("Ctdbd start time              : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
462
463         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
464         seconds = tmp%60;
465         tmp    /= 60;
466         minutes = tmp%60;
467         tmp    /= 60;
468         hours   = tmp%24;
469         tmp    /= 24;
470         days    = tmp;
471         printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
472         
473         printf("Duration of last recovery/failover: %lf seconds\n",
474                 timeval_delta(&uptime->last_recovery_finished,
475                               &uptime->last_recovery_started));
476
477         return 0;
478 }
479
480 /*
481   show the PNN of the current node
482  */
483 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
484 {
485         uint32_t mypnn;
486         bool ret;
487
488         ret = ctdb_getpnn(ctdb_connection, options.pnn, &mypnn);
489         if (!ret) {
490                 DEBUG(DEBUG_ERR, ("Unable to get pnn from node."));
491                 return -1;
492         }
493
494         printf("PNN:%d\n", mypnn);
495         return 0;
496 }
497
498
499 struct pnn_node {
500         struct pnn_node *next;
501         const char *addr;
502         int pnn;
503 };
504
505 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
506 {
507         const char *nodes_list;
508         int nlines;
509         char **lines;
510         int i, pnn;
511         struct pnn_node *pnn_nodes = NULL;
512         struct pnn_node *pnn_node;
513         struct pnn_node *tmp_node;
514
515         /* read the nodes file */
516         nodes_list = getenv("CTDB_NODES");
517         if (nodes_list == NULL) {
518                 nodes_list = "/etc/ctdb/nodes";
519         }
520         lines = file_lines_load(nodes_list, &nlines, mem_ctx);
521         if (lines == NULL) {
522                 return NULL;
523         }
524         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
525                 nlines--;
526         }
527         for (i=0, pnn=0; i<nlines; i++) {
528                 char *node;
529
530                 node = lines[i];
531                 /* strip leading spaces */
532                 while((*node == ' ') || (*node == '\t')) {
533                         node++;
534                 }
535                 if (*node == '#') {
536                         pnn++;
537                         continue;
538                 }
539                 if (strcmp(node, "") == 0) {
540                         continue;
541                 }
542                 pnn_node = talloc(mem_ctx, struct pnn_node);
543                 pnn_node->pnn = pnn++;
544                 pnn_node->addr = talloc_strdup(pnn_node, node);
545                 pnn_node->next = pnn_nodes;
546                 pnn_nodes = pnn_node;
547         }
548
549         /* swap them around so we return them in incrementing order */
550         pnn_node = pnn_nodes;
551         pnn_nodes = NULL;
552         while (pnn_node) {
553                 tmp_node = pnn_node;
554                 pnn_node = pnn_node->next;
555
556                 tmp_node->next = pnn_nodes;
557                 pnn_nodes = tmp_node;
558         }
559
560         return pnn_nodes;
561 }
562
563 /*
564   show the PNN of the current node
565   discover the pnn by loading the nodes file and try to bind to all
566   addresses one at a time until the ip address is found.
567  */
568 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
569 {
570         TALLOC_CTX *mem_ctx = talloc_new(NULL);
571         struct pnn_node *pnn_nodes;
572         struct pnn_node *pnn_node;
573
574         pnn_nodes = read_nodes_file(mem_ctx);
575         if (pnn_nodes == NULL) {
576                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
577                 talloc_free(mem_ctx);
578                 return -1;
579         }
580
581         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
582                 ctdb_sock_addr addr;
583
584                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
585                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
586                         talloc_free(mem_ctx);
587                         return -1;
588                 }
589
590                 if (ctdb_sys_have_ip(&addr)) {
591                         printf("PNN:%d\n", pnn_node->pnn);
592                         talloc_free(mem_ctx);
593                         return 0;
594                 }
595         }
596
597         printf("Failed to detect which PNN this node is\n");
598         talloc_free(mem_ctx);
599         return -1;
600 }
601
602 /*
603   display remote ctdb status
604  */
605 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
606 {
607         int i, ret;
608         struct ctdb_vnn_map *vnnmap=NULL;
609         struct ctdb_node_map *nodemap=NULL;
610         uint32_t recmode, recmaster;
611         int mypnn;
612
613         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
614         if (mypnn == -1) {
615                 return -1;
616         }
617
618         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
619         if (ret != 0) {
620                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
621                 return ret;
622         }
623
624         if(options.machinereadable){
625                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped:Inactive:\n");
626                 for(i=0;i<nodemap->num;i++){
627                         if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
628                                 continue;
629                         }
630                         printf(":%d:%s:%d:%d:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
631                                 ctdb_addr_to_str(&nodemap->nodes[i].addr),
632                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
633                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
634                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
635                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
636                                !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED),
637                                !!(nodemap->nodes[i].flags&NODE_FLAGS_INACTIVE));
638                 }
639                 return 0;
640         }
641
642         printf("Number of nodes:%d\n", nodemap->num);
643         for(i=0;i<nodemap->num;i++){
644                 static const struct {
645                         uint32_t flag;
646                         const char *name;
647                 } flag_names[] = {
648                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
649                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
650                         { NODE_FLAGS_BANNED,                "BANNED" },
651                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
652                         { NODE_FLAGS_DELETED,               "DELETED" },
653                         { NODE_FLAGS_STOPPED,               "STOPPED" },
654                         { NODE_FLAGS_INACTIVE,              "INACTIVE" },
655                 };
656                 char *flags_str = NULL;
657                 int j;
658
659                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
660                         continue;
661                 }
662                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
663                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
664                                 if (flags_str == NULL) {
665                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
666                                 } else {
667                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
668                                                                            flag_names[j].name);
669                                 }
670                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
671                         }
672                 }
673                 if (flags_str == NULL) {
674                         flags_str = talloc_strdup(ctdb, "OK");
675                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
676                 }
677                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
678                        ctdb_addr_to_str(&nodemap->nodes[i].addr),
679                        flags_str,
680                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
681                 talloc_free(flags_str);
682         }
683
684         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
685         if (ret != 0) {
686                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
687                 return ret;
688         }
689         if (vnnmap->generation == INVALID_GENERATION) {
690                 printf("Generation:INVALID\n");
691         } else {
692                 printf("Generation:%d\n",vnnmap->generation);
693         }
694         printf("Size:%d\n",vnnmap->size);
695         for(i=0;i<vnnmap->size;i++){
696                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
697         }
698
699         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
700         if (ret != 0) {
701                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
702                 return ret;
703         }
704         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
705
706         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
707         if (ret != 0) {
708                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
709                 return ret;
710         }
711         printf("Recovery master:%d\n",recmaster);
712
713         return 0;
714 }
715
716
717 struct natgw_node {
718         struct natgw_node *next;
719         const char *addr;
720 };
721
722 /*
723   display the list of nodes belonging to this natgw configuration
724  */
725 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
726 {
727         int i, ret;
728         uint32_t capabilities;
729         const char *natgw_list;
730         int nlines;
731         char **lines;
732         struct natgw_node *natgw_nodes = NULL;
733         struct natgw_node *natgw_node;
734         struct ctdb_node_map *nodemap=NULL;
735
736
737         /* read the natgw nodes file into a linked list */
738         natgw_list = getenv("NATGW_NODES");
739         if (natgw_list == NULL) {
740                 natgw_list = "/etc/ctdb/natgw_nodes";
741         }
742         lines = file_lines_load(natgw_list, &nlines, ctdb);
743         if (lines == NULL) {
744                 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
745                 return -1;
746         }
747         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
748                 nlines--;
749         }
750         for (i=0;i<nlines;i++) {
751                 char *node;
752
753                 node = lines[i];
754                 /* strip leading spaces */
755                 while((*node == ' ') || (*node == '\t')) {
756                         node++;
757                 }
758                 if (*node == '#') {
759                         continue;
760                 }
761                 if (strcmp(node, "") == 0) {
762                         continue;
763                 }
764                 natgw_node = talloc(ctdb, struct natgw_node);
765                 natgw_node->addr = talloc_strdup(natgw_node, node);
766                 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
767                 natgw_node->next = natgw_nodes;
768                 natgw_nodes = natgw_node;
769         }
770
771         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
772         if (ret != 0) {
773                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
774                 return ret;
775         }
776
777         i=0;
778         while(i<nodemap->num) {
779                 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
780                         if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
781                                 break;
782                         }
783                 }
784
785                 /* this node was not in the natgw so we just remove it from
786                  * the list
787                  */
788                 if ((natgw_node == NULL) 
789                 ||  (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
790                         int j;
791
792                         for (j=i+1; j<nodemap->num; j++) {
793                                 nodemap->nodes[j-1] = nodemap->nodes[j];
794                         }
795                         nodemap->num--;
796                         continue;
797                 }
798
799                 i++;
800         }               
801
802         /* pick a node to be natgwmaster
803          * we dont allow STOPPED, DELETED, BANNED or UNHEALTHY nodes to become the natgwmaster
804          */
805         for(i=0;i<nodemap->num;i++){
806                 if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY))) {
807                         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
808                         if (ret != 0) {
809                                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
810                                 return ret;
811                         }
812                         if (!(capabilities&CTDB_CAP_NATGW)) {
813                                 continue;
814                         }
815                         printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
816                         break;
817                 }
818         }
819         /* we couldnt find any healthy node, try unhealthy ones */
820         if (i == nodemap->num) {
821                 for(i=0;i<nodemap->num;i++){
822                         if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED))) {
823                                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
824                                 if (ret != 0) {
825                                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
826                                         return ret;
827                                 }
828                                 if (!(capabilities&CTDB_CAP_NATGW)) {
829                                         continue;
830                                 }
831                                 printf("%d %s\n", nodemap->nodes[i].pnn,ctdb_addr_to_str(&nodemap->nodes[i].addr));
832                                 break;
833                         }
834                 }
835         }
836         /* unless all nodes are STOPPED, when we pick one anyway */
837         if (i == nodemap->num) {
838                 for(i=0;i<nodemap->num;i++){
839                         if (!(nodemap->nodes[i].flags & (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED))) {
840                                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, &capabilities);
841                                 if (ret != 0) {
842                                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", nodemap->nodes[i].pnn));
843                                         return ret;
844                                 }
845                                 if (!(capabilities&CTDB_CAP_NATGW)) {
846                                         continue;
847                                 }
848                                 printf("%d %s\n", nodemap->nodes[i].pnn, ctdb_addr_to_str(&nodemap->nodes[i].addr));
849                                 break;
850                         }
851                 }
852                 /* or if we still can not find any */
853                 if (i == nodemap->num) {
854                         printf("-1 0.0.0.0\n");
855                 }
856         }
857
858         /* print the pruned list of nodes belonging to this natgw list */
859         for(i=0;i<nodemap->num;i++){
860                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
861                         continue;
862                 }
863                 printf(":%d:%s:%d:%d:%d:%d:%d\n", nodemap->nodes[i].pnn,
864                         ctdb_addr_to_str(&nodemap->nodes[i].addr),
865                        !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
866                        !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
867                        !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
868                        !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY),
869                        !!(nodemap->nodes[i].flags&NODE_FLAGS_STOPPED));
870         }
871
872         return 0;
873 }
874
875 /*
876   display the status of the scripts for monitoring (or other events)
877  */
878 static int control_one_scriptstatus(struct ctdb_context *ctdb,
879                                     enum ctdb_eventscript_call type)
880 {
881         struct ctdb_scripts_wire *script_status;
882         int ret, i;
883
884         ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
885         if (ret != 0) {
886                 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
887                 return ret;
888         }
889
890         if (script_status == NULL) {
891                 if (!options.machinereadable) {
892                         printf("%s cycle never run\n",
893                                ctdb_eventscript_call_names[type]);
894                 }
895                 return 0;
896         }
897
898         if (!options.machinereadable) {
899                 printf("%d scripts were executed last %s cycle\n",
900                        script_status->num_scripts,
901                        ctdb_eventscript_call_names[type]);
902         }
903         for (i=0; i<script_status->num_scripts; i++) {
904                 const char *status = NULL;
905
906                 switch (script_status->scripts[i].status) {
907                 case -ETIME:
908                         status = "TIMEDOUT";
909                         break;
910                 case -ENOEXEC:
911                         status = "DISABLED";
912                         break;
913                 case 0:
914                         status = "OK";
915                         break;
916                 default:
917                         if (script_status->scripts[i].status > 0)
918                                 status = "ERROR";
919                         break;
920                 }
921                 if (options.machinereadable) {
922                         printf("%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
923                                ctdb_eventscript_call_names[type],
924                                script_status->scripts[i].name,
925                                script_status->scripts[i].status,
926                                status,
927                                (long)script_status->scripts[i].start.tv_sec,
928                                (long)script_status->scripts[i].start.tv_usec,
929                                (long)script_status->scripts[i].finished.tv_sec,
930                                (long)script_status->scripts[i].finished.tv_usec,
931                                script_status->scripts[i].output);
932                         continue;
933                 }
934                 if (status)
935                         printf("%-20s Status:%s    ",
936                                script_status->scripts[i].name, status);
937                 else
938                         /* Some other error, eg from stat. */
939                         printf("%-20s Status:CANNOT RUN (%s)",
940                                script_status->scripts[i].name,
941                                strerror(-script_status->scripts[i].status));
942
943                 if (script_status->scripts[i].status >= 0) {
944                         printf("Duration:%.3lf ",
945                         timeval_delta(&script_status->scripts[i].finished,
946                               &script_status->scripts[i].start));
947                 }
948                 if (script_status->scripts[i].status != -ENOEXEC) {
949                         printf("%s",
950                                ctime(&script_status->scripts[i].start.tv_sec));
951                         if (script_status->scripts[i].status != 0) {
952                                 printf("   OUTPUT:%s\n",
953                                        script_status->scripts[i].output);
954                         }
955                 } else {
956                         printf("\n");
957                 }
958         }
959         return 0;
960 }
961
962
963 static int control_scriptstatus(struct ctdb_context *ctdb,
964                                 int argc, const char **argv)
965 {
966         int ret;
967         enum ctdb_eventscript_call type, min, max;
968         const char *arg;
969
970         if (argc > 1) {
971                 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
972                 return -1;
973         }
974
975         if (argc == 0)
976                 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
977         else
978                 arg = argv[0];
979
980         for (type = 0; type < CTDB_EVENT_MAX; type++) {
981                 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
982                         min = type;
983                         max = type+1;
984                         break;
985                 }
986         }
987         if (type == CTDB_EVENT_MAX) {
988                 if (strcmp(arg, "all") == 0) {
989                         min = 0;
990                         max = CTDB_EVENT_MAX;
991                 } else {
992                         DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
993                         return -1;
994                 }
995         }
996
997         if (options.machinereadable) {
998                 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
999         }
1000
1001         for (type = min; type < max; type++) {
1002                 ret = control_one_scriptstatus(ctdb, type);
1003                 if (ret != 0) {
1004                         return ret;
1005                 }
1006         }
1007
1008         return 0;
1009 }
1010
1011 /*
1012   enable an eventscript
1013  */
1014 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1015 {
1016         int ret;
1017
1018         if (argc < 1) {
1019                 usage();
1020         }
1021
1022         ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1023         if (ret != 0) {
1024           DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1025                 return ret;
1026         }
1027
1028         return 0;
1029 }
1030
1031 /*
1032   disable an eventscript
1033  */
1034 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1035 {
1036         int ret;
1037
1038         if (argc < 1) {
1039                 usage();
1040         }
1041
1042         ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1043         if (ret != 0) {
1044           DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1045                 return ret;
1046         }
1047
1048         return 0;
1049 }
1050
1051 /*
1052   display the pnn of the recovery master
1053  */
1054 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1055 {
1056         int ret;
1057         uint32_t recmaster;
1058
1059         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1060         if (ret != 0) {
1061                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1062                 return ret;
1063         }
1064         printf("%d\n",recmaster);
1065
1066         return 0;
1067 }
1068
1069 /*
1070   add a tickle to a public address
1071  */
1072 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1073 {
1074         struct ctdb_tcp_connection t;
1075         TDB_DATA data;
1076         int ret;
1077
1078         if (argc < 2) {
1079                 usage();
1080         }
1081
1082         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1083                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1084                 return -1;
1085         }
1086         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1087                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1088                 return -1;
1089         }
1090
1091         data.dptr = (uint8_t *)&t;
1092         data.dsize = sizeof(t);
1093
1094         /* tell all nodes about this tcp connection */
1095         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1096                            0, data, ctdb, NULL, NULL, NULL, NULL);
1097         if (ret != 0) {
1098                 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1099                 return -1;
1100         }
1101         
1102         return 0;
1103 }
1104
1105
1106 /*
1107   delete a tickle from a node
1108  */
1109 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1110 {
1111         struct ctdb_tcp_connection t;
1112         TDB_DATA data;
1113         int ret;
1114
1115         if (argc < 2) {
1116                 usage();
1117         }
1118
1119         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1120                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1121                 return -1;
1122         }
1123         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1124                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1125                 return -1;
1126         }
1127
1128         data.dptr = (uint8_t *)&t;
1129         data.dsize = sizeof(t);
1130
1131         /* tell all nodes about this tcp connection */
1132         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1133                            0, data, ctdb, NULL, NULL, NULL, NULL);
1134         if (ret != 0) {
1135                 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1136                 return -1;
1137         }
1138         
1139         return 0;
1140 }
1141
1142
1143 /*
1144   get a list of all tickles for this pnn
1145  */
1146 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1147 {
1148         struct ctdb_control_tcp_tickle_list *list;
1149         ctdb_sock_addr addr;
1150         int i, ret;
1151         unsigned port = 0;
1152
1153         if (argc < 1) {
1154                 usage();
1155         }
1156
1157         if (argc == 2) {
1158                 port = atoi(argv[1]);
1159         }
1160
1161         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1162                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1163                 return -1;
1164         }
1165
1166         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1167         if (ret == -1) {
1168                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1169                 return -1;
1170         }
1171
1172         if (options.machinereadable){
1173                 printf(":source ip:port:destination ip:port:\n");
1174                 for (i=0;i<list->tickles.num;i++) {
1175                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1176                                 continue;
1177                         }
1178                         printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1179                         printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1180                 }
1181         } else {
1182                 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1183                 printf("Num tickles:%u\n", list->tickles.num);
1184                 for (i=0;i<list->tickles.num;i++) {
1185                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1186                                 continue;
1187                         }
1188                         printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1189                         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));
1190                 }
1191         }
1192
1193         talloc_free(list);
1194         
1195         return 0;
1196 }
1197
1198
1199 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1200 {
1201         struct ctdb_all_public_ips *ips;
1202         struct ctdb_public_ip ip;
1203         int i, ret;
1204         uint32_t *nodes;
1205         uint32_t disable_time;
1206         TDB_DATA data;
1207         struct ctdb_node_map *nodemap=NULL;
1208         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1209
1210         disable_time = 30;
1211         data.dptr  = (uint8_t*)&disable_time;
1212         data.dsize = sizeof(disable_time);
1213         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1214         if (ret != 0) {
1215                 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1216                 return -1;
1217         }
1218
1219
1220
1221         /* read the public ip list from the node */
1222         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1223         if (ret != 0) {
1224                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1225                 talloc_free(tmp_ctx);
1226                 return -1;
1227         }
1228
1229         for (i=0;i<ips->num;i++) {
1230                 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1231                         break;
1232                 }
1233         }
1234         if (i==ips->num) {
1235                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1236                         pnn, ctdb_addr_to_str(addr)));
1237                 talloc_free(tmp_ctx);
1238                 return -1;
1239         }
1240
1241         ip.pnn  = pnn;
1242         ip.addr = *addr;
1243
1244         data.dptr  = (uint8_t *)&ip;
1245         data.dsize = sizeof(ip);
1246
1247         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1248         if (ret != 0) {
1249                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1250                 talloc_free(tmp_ctx);
1251                 return ret;
1252         }
1253
1254         nodes = list_of_active_nodes_except_pnn(ctdb, nodemap, tmp_ctx, pnn);
1255         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1256                                         nodes, 0,
1257                                         LONGTIMELIMIT(),
1258                                         false, data,
1259                                         NULL, NULL,
1260                                         NULL);
1261         if (ret != 0) {
1262                 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1263                 talloc_free(tmp_ctx);
1264                 return -1;
1265         }
1266
1267         ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1268         if (ret != 0) {
1269                 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1270                 talloc_free(tmp_ctx);
1271                 return -1;
1272         }
1273
1274         /* update the recovery daemon so it now knows to expect the new
1275            node assignment for this ip.
1276         */
1277         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1278         if (ret != 0) {
1279                 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1280                 return -1;
1281         }
1282
1283         talloc_free(tmp_ctx);
1284         return 0;
1285 }
1286
1287 /*
1288   move/failover an ip address to a specific node
1289  */
1290 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1291 {
1292         uint32_t pnn;
1293         int ret, retries = 0;
1294         ctdb_sock_addr addr;
1295
1296         if (argc < 2) {
1297                 usage();
1298                 return -1;
1299         }
1300
1301         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1302                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1303                 return -1;
1304         }
1305
1306
1307         if (sscanf(argv[1], "%u", &pnn) != 1) {
1308                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1309                 return -1;
1310         }
1311
1312         do {
1313                 ret = move_ip(ctdb, &addr, pnn);
1314                 if (ret != 0) {
1315                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 second and try again.\n", pnn));
1316                         sleep(3);
1317                         retries++;
1318                 }
1319         } while (retries < 5 && ret != 0);
1320         if (ret != 0) {
1321                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1322                 return -1;
1323         }
1324
1325         return 0;
1326 }
1327
1328 void getips_store_callback(void *param, void *data)
1329 {
1330         struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1331         struct ctdb_all_public_ips *ips = param;
1332         int i;
1333
1334         i = ips->num++;
1335         ips->ips[i].pnn  = node_ip->pnn;
1336         ips->ips[i].addr = node_ip->addr;
1337 }
1338
1339 void getips_count_callback(void *param, void *data)
1340 {
1341         uint32_t *count = param;
1342
1343         (*count)++;
1344 }
1345
1346 #define IP_KEYLEN       4
1347 static uint32_t *ip_key(ctdb_sock_addr *ip)
1348 {
1349         static uint32_t key[IP_KEYLEN];
1350
1351         bzero(key, sizeof(key));
1352
1353         switch (ip->sa.sa_family) {
1354         case AF_INET:
1355                 key[0]  = ip->ip.sin_addr.s_addr;
1356                 break;
1357         case AF_INET6:
1358                 key[0]  = ip->ip6.sin6_addr.s6_addr32[3];
1359                 key[1]  = ip->ip6.sin6_addr.s6_addr32[2];
1360                 key[2]  = ip->ip6.sin6_addr.s6_addr32[1];
1361                 key[3]  = ip->ip6.sin6_addr.s6_addr32[0];
1362                 break;
1363         default:
1364                 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1365                 return key;
1366         }
1367
1368         return key;
1369 }
1370
1371 static void *add_ip_callback(void *parm, void *data)
1372 {
1373         return parm;
1374 }
1375
1376 static int
1377 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1378 {
1379         struct ctdb_all_public_ips *tmp_ips;
1380         struct ctdb_node_map *nodemap=NULL;
1381         trbt_tree_t *ip_tree;
1382         int i, j, len, ret;
1383         uint32_t count;
1384
1385         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1386         if (ret != 0) {
1387                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1388                 return ret;
1389         }
1390
1391         ip_tree = trbt_create(tmp_ctx, 0);
1392
1393         for(i=0;i<nodemap->num;i++){
1394                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1395                         continue;
1396                 }
1397                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
1398                         continue;
1399                 }
1400
1401                 /* read the public ip list from this node */
1402                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
1403                 if (ret != 0) {
1404                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1405                         return -1;
1406                 }
1407         
1408                 for (j=0; j<tmp_ips->num;j++) {
1409                         struct ctdb_public_ip *node_ip;
1410
1411                         node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
1412                         node_ip->pnn  = tmp_ips->ips[j].pnn;
1413                         node_ip->addr = tmp_ips->ips[j].addr;
1414
1415                         trbt_insertarray32_callback(ip_tree,
1416                                 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
1417                                 add_ip_callback,
1418                                 node_ip);
1419                 }
1420                 talloc_free(tmp_ips);
1421         }
1422
1423         /* traverse */
1424         count = 0;
1425         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
1426
1427         len = offsetof(struct ctdb_all_public_ips, ips) + 
1428                 count*sizeof(struct ctdb_public_ip);
1429         tmp_ips = talloc_zero_size(tmp_ctx, len);
1430         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
1431
1432         *ips = tmp_ips;
1433
1434         return 0;
1435 }
1436
1437
1438 /* 
1439  * scans all other nodes and returns a pnn for another node that can host this 
1440  * ip address or -1
1441  */
1442 static int
1443 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1444 {
1445         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1446         struct ctdb_all_public_ips *ips;
1447         struct ctdb_node_map *nodemap=NULL;
1448         int i, j, ret;
1449
1450         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1451         if (ret != 0) {
1452                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1453                 talloc_free(tmp_ctx);
1454                 return ret;
1455         }
1456
1457         for(i=0;i<nodemap->num;i++){
1458                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1459                         continue;
1460                 }
1461                 if (nodemap->nodes[i].pnn == options.pnn) {
1462                         continue;
1463                 }
1464
1465                 /* read the public ip list from this node */
1466                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1467                 if (ret != 0) {
1468                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1469                         return -1;
1470                 }
1471
1472                 for (j=0;j<ips->num;j++) {
1473                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1474                                 talloc_free(tmp_ctx);
1475                                 return nodemap->nodes[i].pnn;
1476                         }
1477                 }
1478                 talloc_free(ips);
1479         }
1480
1481         talloc_free(tmp_ctx);
1482         return -1;
1483 }
1484
1485 static uint32_t ipreallocate_finished;
1486
1487 /*
1488   handler for receiving the response to ipreallocate
1489 */
1490 static void ip_reallocate_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1491                              TDB_DATA data, void *private_data)
1492 {
1493         ipreallocate_finished = 1;
1494 }
1495
1496 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
1497 {
1498         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
1499
1500         event_add_timed(ctdb->ev, ctdb, 
1501                                 timeval_current_ofs(1, 0),
1502                                 ctdb_every_second, ctdb);
1503 }
1504
1505 /*
1506   ask the recovery daemon on the recovery master to perform a ip reallocation
1507  */
1508 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
1509 {
1510         int i, ret;
1511         TDB_DATA data;
1512         struct takeover_run_reply rd;
1513         uint32_t recmaster;
1514         struct ctdb_node_map *nodemap=NULL;
1515         int retries=0;
1516         struct timeval tv = timeval_current();
1517
1518         /* we need some events to trigger so we can timeout and restart
1519            the loop
1520         */
1521         event_add_timed(ctdb->ev, ctdb, 
1522                                 timeval_current_ofs(1, 0),
1523                                 ctdb_every_second, ctdb);
1524
1525         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1526         if (rd.pnn == -1) {
1527                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1528                 return -1;
1529         }
1530         rd.srvid = getpid();
1531
1532         /* register a message port for receiveing the reply so that we
1533            can receive the reply
1534         */
1535         ctdb_client_set_message_handler(ctdb, rd.srvid, ip_reallocate_handler, NULL);
1536
1537         data.dptr = (uint8_t *)&rd;
1538         data.dsize = sizeof(rd);
1539
1540 again:
1541         /* check that there are valid nodes available */
1542         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap) != 0) {
1543                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1544                 return -1;
1545         }
1546         for (i=0; i<nodemap->num;i++) {
1547                 if ((nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) == 0) {
1548                         break;
1549                 }
1550         }
1551         if (i==nodemap->num) {
1552                 DEBUG(DEBUG_ERR,("No recmaster available, no need to wait for cluster convergence\n"));
1553                 return 0;
1554         }
1555
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 ret;
1561         }
1562
1563         /* verify the node exists */
1564         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), recmaster, ctdb, &nodemap) != 0) {
1565                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1566                 return -1;
1567         }
1568
1569
1570         /* check tha there are nodes available that can act as a recmaster */
1571         for (i=0; i<nodemap->num; i++) {
1572                 if (nodemap->nodes[i].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1573                         continue;
1574                 }
1575                 break;
1576         }
1577         if (i == nodemap->num) {
1578                 DEBUG(DEBUG_ERR,("No possible nodes to host addresses.\n"));
1579                 return 0;
1580         }
1581
1582         /* verify the recovery master is not STOPPED, nor BANNED */
1583         if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1584                 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1585                 retries++;
1586                 sleep(1);
1587                 goto again;
1588         } 
1589         
1590         /* verify the recovery master is not STOPPED, nor BANNED */
1591         if (nodemap->nodes[recmaster].flags & (NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)) {
1592                 DEBUG(DEBUG_ERR,("No suitable recmaster found. Try again\n"));
1593                 retries++;
1594                 sleep(1);
1595                 goto again;
1596         } 
1597
1598         ipreallocate_finished = 0;
1599         ret = ctdb_client_send_message(ctdb, recmaster, CTDB_SRVID_TAKEOVER_RUN, data);
1600         if (ret != 0) {
1601                 DEBUG(DEBUG_ERR,("Failed to send ip takeover run request message to %u\n", options.pnn));
1602                 return -1;
1603         }
1604
1605         tv = timeval_current();
1606         /* this loop will terminate when we have received the reply */
1607         while (timeval_elapsed(&tv) < 5.0 && ipreallocate_finished == 0) {
1608                 event_loop_once(ctdb->ev);
1609         }
1610         if (ipreallocate_finished == 1) {
1611                 return 0;
1612         }
1613
1614         retries++;
1615         sleep(1);
1616         goto again;
1617
1618         return 0;
1619 }
1620
1621
1622 /*
1623   add a public ip address to a node
1624  */
1625 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
1626 {
1627         int i, ret;
1628         int len, retries = 0;
1629         uint32_t pnn;
1630         unsigned mask;
1631         ctdb_sock_addr addr;
1632         struct ctdb_control_ip_iface *pub;
1633         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1634         struct ctdb_all_public_ips *ips;
1635
1636
1637         if (argc != 2) {
1638                 talloc_free(tmp_ctx);
1639                 usage();
1640         }
1641
1642         if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1643                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1644                 talloc_free(tmp_ctx);
1645                 return -1;
1646         }
1647
1648         ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1649         if (ret != 0) {
1650                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1651                 talloc_free(tmp_ctx);
1652                 return ret;
1653         }
1654
1655
1656         /* check if some other node is already serving this ip, if not,
1657          * we will claim it
1658          */
1659         for (i=0;i<ips->num;i++) {
1660                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1661                         break;
1662                 }
1663         }
1664
1665         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1666         pub = talloc_size(tmp_ctx, len); 
1667         CTDB_NO_MEMORY(ctdb, pub);
1668
1669         pub->addr  = addr;
1670         pub->mask  = mask;
1671         pub->len   = strlen(argv[1])+1;
1672         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1673
1674         do {
1675                 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1676                 if (ret != 0) {
1677                         DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1678                         sleep(3);
1679                         retries++;
1680                 }
1681         } while (retries < 5 && ret != 0);
1682         if (ret != 0) {
1683                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1684                 talloc_free(tmp_ctx);
1685                 return ret;
1686         }
1687
1688         if (i == ips->num) {
1689                 /* no one has this ip so we claim it */
1690                 pnn  = options.pnn;
1691         } else {
1692                 pnn  = ips->ips[i].pnn;
1693         }
1694
1695         do {
1696                 ret = move_ip(ctdb, &addr, pnn);
1697                 if (ret != 0) {
1698                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. wait 3 seconds and try again.\n", pnn));
1699                         sleep(3);
1700                         retries++;
1701                 }
1702         } while (retries < 5 && ret != 0);
1703         if (ret != 0) {
1704                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", pnn));
1705                 talloc_free(tmp_ctx);
1706                 return ret;
1707         }
1708
1709         do {
1710                 ret = control_ipreallocate(ctdb, argc, argv);
1711                 if (ret != 0) {
1712                         DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1713                         sleep(3);
1714                         retries++;
1715                 }
1716         } while (retries < 5 && ret != 0);
1717         if (ret != 0) {
1718                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1719                 talloc_free(tmp_ctx);
1720                 return ret;
1721         }
1722
1723         talloc_free(tmp_ctx);
1724         return 0;
1725 }
1726
1727 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1728
1729 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1730 {
1731         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1732         struct ctdb_node_map *nodemap=NULL;
1733         struct ctdb_all_public_ips *ips;
1734         int ret, i, j;
1735
1736         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1737         if (ret != 0) {
1738                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1739                 return ret;
1740         }
1741
1742         /* remove it from the nodes that are not hosting the ip currently */
1743         for(i=0;i<nodemap->num;i++){
1744                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1745                         continue;
1746                 }
1747                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1748                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1749                         continue;
1750                 }
1751
1752                 for (j=0;j<ips->num;j++) {
1753                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1754                                 break;
1755                         }
1756                 }
1757                 if (j==ips->num) {
1758                         continue;
1759                 }
1760
1761                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1762                         continue;
1763                 }
1764
1765                 options.pnn = nodemap->nodes[i].pnn;
1766                 control_delip(ctdb, argc, argv);
1767         }
1768
1769
1770         /* remove it from every node (also the one hosting it) */
1771         for(i=0;i<nodemap->num;i++){
1772                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1773                         continue;
1774                 }
1775                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1776                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1777                         continue;
1778                 }
1779
1780                 for (j=0;j<ips->num;j++) {
1781                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1782                                 break;
1783                         }
1784                 }
1785                 if (j==ips->num) {
1786                         continue;
1787                 }
1788
1789                 options.pnn = nodemap->nodes[i].pnn;
1790                 control_delip(ctdb, argc, argv);
1791         }
1792
1793         talloc_free(tmp_ctx);
1794         return 0;
1795 }
1796         
1797 /*
1798   delete a public ip address from a node
1799  */
1800 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1801 {
1802         int i, ret;
1803         int retries = 0;
1804         ctdb_sock_addr addr;
1805         struct ctdb_control_ip_iface pub;
1806         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1807         struct ctdb_all_public_ips *ips;
1808
1809         if (argc != 1) {
1810                 talloc_free(tmp_ctx);
1811                 usage();
1812         }
1813
1814         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1815                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1816                 return -1;
1817         }
1818
1819         if (options.pnn == CTDB_BROADCAST_ALL) {
1820                 return control_delip_all(ctdb, argc, argv, &addr);
1821         }
1822
1823         pub.addr  = addr;
1824         pub.mask  = 0;
1825         pub.len   = 0;
1826
1827         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1828         if (ret != 0) {
1829                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1830                 talloc_free(tmp_ctx);
1831                 return ret;
1832         }
1833         
1834         for (i=0;i<ips->num;i++) {
1835                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1836                         break;
1837                 }
1838         }
1839
1840         if (i==ips->num) {
1841                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1842                         ctdb_addr_to_str(&addr)));
1843                 talloc_free(tmp_ctx);
1844                 return -1;
1845         }
1846
1847         if (ips->ips[i].pnn == options.pnn) {
1848                 ret = find_other_host_for_public_ip(ctdb, &addr);
1849                 if (ret != -1) {
1850                         do {
1851                                 ret = move_ip(ctdb, &addr, ret);
1852                                 if (ret != 0) {
1853                                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1854                                         sleep(3);
1855                                         retries++;
1856                                 }
1857                         } while (retries < 5 && ret != 0);
1858                         if (ret != 0) {
1859                                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1860                                 return -1;
1861                         }
1862                 }
1863         }
1864
1865         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1866         if (ret != 0) {
1867                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1868                 talloc_free(tmp_ctx);
1869                 return ret;
1870         }
1871
1872         talloc_free(tmp_ctx);
1873         return 0;
1874 }
1875
1876 /*
1877   kill a tcp connection
1878  */
1879 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1880 {
1881         int ret;
1882         struct ctdb_control_killtcp killtcp;
1883
1884         if (argc < 2) {
1885                 usage();
1886         }
1887
1888         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1889                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1890                 return -1;
1891         }
1892
1893         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1894                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1895                 return -1;
1896         }
1897
1898         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1899         if (ret != 0) {
1900                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1901                 return ret;
1902         }
1903
1904         return 0;
1905 }
1906
1907
1908 /*
1909   send a gratious arp
1910  */
1911 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1912 {
1913         int ret;
1914         ctdb_sock_addr addr;
1915
1916         if (argc < 2) {
1917                 usage();
1918         }
1919
1920         if (!parse_ip(argv[0], NULL, 0, &addr)) {
1921                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1922                 return -1;
1923         }
1924
1925         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1926         if (ret != 0) {
1927                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1928                 return ret;
1929         }
1930
1931         return 0;
1932 }
1933
1934 /*
1935   register a server id
1936  */
1937 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1938 {
1939         int ret;
1940         struct ctdb_server_id server_id;
1941
1942         if (argc < 3) {
1943                 usage();
1944         }
1945
1946         server_id.pnn       = strtoul(argv[0], NULL, 0);
1947         server_id.type      = strtoul(argv[1], NULL, 0);
1948         server_id.server_id = strtoul(argv[2], NULL, 0);
1949
1950         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1951         if (ret != 0) {
1952                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1953                 return ret;
1954         }
1955         DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1956         sleep(999);
1957         return -1;
1958 }
1959
1960 /*
1961   unregister a server id
1962  */
1963 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1964 {
1965         int ret;
1966         struct ctdb_server_id server_id;
1967
1968         if (argc < 3) {
1969                 usage();
1970         }
1971
1972         server_id.pnn       = strtoul(argv[0], NULL, 0);
1973         server_id.type      = strtoul(argv[1], NULL, 0);
1974         server_id.server_id = strtoul(argv[2], NULL, 0);
1975
1976         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1977         if (ret != 0) {
1978                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1979                 return ret;
1980         }
1981         return -1;
1982 }
1983
1984 /*
1985   check if a server id exists
1986  */
1987 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1988 {
1989         uint32_t status;
1990         int ret;
1991         struct ctdb_server_id server_id;
1992
1993         if (argc < 3) {
1994                 usage();
1995         }
1996
1997         server_id.pnn       = strtoul(argv[0], NULL, 0);
1998         server_id.type      = strtoul(argv[1], NULL, 0);
1999         server_id.server_id = strtoul(argv[2], NULL, 0);
2000
2001         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2002         if (ret != 0) {
2003                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2004                 return ret;
2005         }
2006
2007         if (status) {
2008                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2009         } else {
2010                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2011         }
2012         return 0;
2013 }
2014
2015 /*
2016   get a list of all server ids that are registered on a node
2017  */
2018 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2019 {
2020         int i, ret;
2021         struct ctdb_server_id_list *server_ids;
2022
2023         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2024         if (ret != 0) {
2025                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2026                 return ret;
2027         }
2028
2029         for (i=0; i<server_ids->num; i++) {
2030                 printf("Server id %d:%d:%d\n", 
2031                         server_ids->server_ids[i].pnn, 
2032                         server_ids->server_ids[i].type, 
2033                         server_ids->server_ids[i].server_id); 
2034         }
2035
2036         return -1;
2037 }
2038
2039 /*
2040   send a tcp tickle ack
2041  */
2042 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2043 {
2044         int ret;
2045         ctdb_sock_addr  src, dst;
2046
2047         if (argc < 2) {
2048                 usage();
2049         }
2050
2051         if (!parse_ip_port(argv[0], &src)) {
2052                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2053                 return -1;
2054         }
2055
2056         if (!parse_ip_port(argv[1], &dst)) {
2057                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2058                 return -1;
2059         }
2060
2061         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2062         if (ret==0) {
2063                 return 0;
2064         }
2065         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2066
2067         return -1;
2068 }
2069
2070
2071 /*
2072   display public ip status
2073  */
2074 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2075 {
2076         int i, ret;
2077         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2078         struct ctdb_all_public_ips *ips;
2079
2080         if (options.pnn == CTDB_BROADCAST_ALL) {
2081                 /* read the list of public ips from all nodes */
2082                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2083         } else {
2084                 /* read the public ip list from this node */
2085                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2086         }
2087         if (ret != 0) {
2088                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2089                 talloc_free(tmp_ctx);
2090                 return ret;
2091         }
2092
2093         if (options.machinereadable){
2094                 printf(":Public IP:Node:");
2095                 if (options.verbose){
2096                         printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2097                 }
2098                 printf("\n");
2099         } else {
2100                 if (options.pnn == CTDB_BROADCAST_ALL) {
2101                         printf("Public IPs on ALL nodes\n");
2102                 } else {
2103                         printf("Public IPs on node %u\n", options.pnn);
2104                 }
2105         }
2106
2107         for (i=1;i<=ips->num;i++) {
2108                 struct ctdb_control_public_ip_info *info = NULL;
2109                 int32_t pnn;
2110                 char *aciface = NULL;
2111                 char *avifaces = NULL;
2112                 char *cifaces = NULL;
2113
2114                 if (options.pnn == CTDB_BROADCAST_ALL) {
2115                         pnn = ips->ips[ips->num-i].pnn;
2116                 } else {
2117                         pnn = options.pnn;
2118                 }
2119
2120                 if (pnn != -1) {
2121                         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2122                                                    &ips->ips[ips->num-i].addr, &info);
2123                 } else {
2124                         ret = -1;
2125                 }
2126
2127                 if (ret == 0) {
2128                         int j;
2129                         for (j=0; j < info->num; j++) {
2130                                 if (cifaces == NULL) {
2131                                         cifaces = talloc_strdup(info,
2132                                                                 info->ifaces[j].name);
2133                                 } else {
2134                                         cifaces = talloc_asprintf_append(cifaces,
2135                                                                          ",%s",
2136                                                                          info->ifaces[j].name);
2137                                 }
2138
2139                                 if (info->active_idx == j) {
2140                                         aciface = info->ifaces[j].name;
2141                                 }
2142
2143                                 if (info->ifaces[j].link_state == 0) {
2144                                         continue;
2145                                 }
2146
2147                                 if (avifaces == NULL) {
2148                                         avifaces = talloc_strdup(info, info->ifaces[j].name);
2149                                 } else {
2150                                         avifaces = talloc_asprintf_append(avifaces,
2151                                                                           ",%s",
2152                                                                           info->ifaces[j].name);
2153                                 }
2154                         }
2155                 }
2156
2157                 if (options.machinereadable){
2158                         printf(":%s:%d:",
2159                                 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2160                                 ips->ips[ips->num-i].pnn);
2161                         if (options.verbose){
2162                                 printf("%s:%s:%s:",
2163                                         aciface?aciface:"",
2164                                         avifaces?avifaces:"",
2165                                         cifaces?cifaces:"");
2166                         }
2167                         printf("\n");
2168                 } else {
2169                         if (options.verbose) {
2170                                 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2171                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2172                                         ips->ips[ips->num-i].pnn,
2173                                         aciface?aciface:"",
2174                                         avifaces?avifaces:"",
2175                                         cifaces?cifaces:"");
2176                         } else {
2177                                 printf("%s %d\n",
2178                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2179                                         ips->ips[ips->num-i].pnn);
2180                         }
2181                 }
2182                 talloc_free(info);
2183         }
2184
2185         talloc_free(tmp_ctx);
2186         return 0;
2187 }
2188
2189 /*
2190   public ip info
2191  */
2192 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2193 {
2194         int i, ret;
2195         ctdb_sock_addr addr;
2196         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2197         struct ctdb_control_public_ip_info *info;
2198
2199         if (argc != 1) {
2200                 talloc_free(tmp_ctx);
2201                 usage();
2202         }
2203
2204         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2205                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2206                 return -1;
2207         }
2208
2209         /* read the public ip info from this node */
2210         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2211                                            tmp_ctx, &addr, &info);
2212         if (ret != 0) {
2213                 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2214                                   argv[0], options.pnn));
2215                 talloc_free(tmp_ctx);
2216                 return ret;
2217         }
2218
2219         printf("Public IP[%s] info on node %u\n",
2220                ctdb_addr_to_str(&info->ip.addr),
2221                options.pnn);
2222
2223         printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2224                ctdb_addr_to_str(&info->ip.addr),
2225                info->ip.pnn, info->num);
2226
2227         for (i=0; i<info->num; i++) {
2228                 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2229
2230                 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2231                        i+1, info->ifaces[i].name,
2232                        info->ifaces[i].link_state?"up":"down",
2233                        (unsigned int)info->ifaces[i].references,
2234                        (i==info->active_idx)?" (active)":"");
2235         }
2236
2237         talloc_free(tmp_ctx);
2238         return 0;
2239 }
2240
2241 /*
2242   display interfaces status
2243  */
2244 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2245 {
2246         int i, ret;
2247         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2248         struct ctdb_control_get_ifaces *ifaces;
2249
2250         /* read the public ip list from this node */
2251         ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2252                                    tmp_ctx, &ifaces);
2253         if (ret != 0) {
2254                 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2255                                   options.pnn));
2256                 talloc_free(tmp_ctx);
2257                 return ret;
2258         }
2259
2260         if (options.machinereadable){
2261                 printf(":Name:LinkStatus:References:\n");
2262         } else {
2263                 printf("Interfaces on node %u\n", options.pnn);
2264         }
2265
2266         for (i=0; i<ifaces->num; i++) {
2267                 if (options.machinereadable){
2268                         printf(":%s:%s:%u\n",
2269                                ifaces->ifaces[i].name,
2270                                ifaces->ifaces[i].link_state?"1":"0",
2271                                (unsigned int)ifaces->ifaces[i].references);
2272                 } else {
2273                         printf("name:%s link:%s references:%u\n",
2274                                ifaces->ifaces[i].name,
2275                                ifaces->ifaces[i].link_state?"up":"down",
2276                                (unsigned int)ifaces->ifaces[i].references);
2277                 }
2278         }
2279
2280         talloc_free(tmp_ctx);
2281         return 0;
2282 }
2283
2284
2285 /*
2286   set link status of an interface
2287  */
2288 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2289 {
2290         int ret;
2291         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2292         struct ctdb_control_iface_info info;
2293
2294         ZERO_STRUCT(info);
2295
2296         if (argc != 2) {
2297                 usage();
2298         }
2299
2300         if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2301                 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2302                                   argv[0]));
2303                 talloc_free(tmp_ctx);
2304                 return -1;
2305         }
2306         strcpy(info.name, argv[0]);
2307
2308         if (strcmp(argv[1], "up") == 0) {
2309                 info.link_state = 1;
2310         } else if (strcmp(argv[1], "down") == 0) {
2311                 info.link_state = 0;
2312         } else {
2313                 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2314                                   argv[1]));
2315                 talloc_free(tmp_ctx);
2316                 return -1;
2317         }
2318
2319         /* read the public ip list from this node */
2320         ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2321                                    tmp_ctx, &info);
2322         if (ret != 0) {
2323                 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2324                                   argv[0], options.pnn));
2325                 talloc_free(tmp_ctx);
2326                 return ret;
2327         }
2328
2329         talloc_free(tmp_ctx);
2330         return 0;
2331 }
2332
2333 /*
2334   display pid of a ctdb daemon
2335  */
2336 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2337 {
2338         uint32_t pid;
2339         int ret;
2340
2341         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2342         if (ret != 0) {
2343                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2344                 return ret;
2345         }
2346         printf("Pid:%d\n", pid);
2347
2348         return 0;
2349 }
2350
2351 /*
2352   disable a remote node
2353  */
2354 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2355 {
2356         int ret;
2357         struct ctdb_node_map *nodemap=NULL;
2358
2359         /* check if the node is already disabled */
2360         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2361                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2362                 exit(10);
2363         }
2364         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2365                 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2366                 return 0;
2367         }
2368
2369         do {
2370                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2371                 if (ret != 0) {
2372                         DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2373                         return ret;
2374                 }
2375
2376                 sleep(1);
2377
2378                 /* read the nodemap and verify the change took effect */
2379                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2380                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2381                         exit(10);
2382                 }
2383
2384         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2385         ret = control_ipreallocate(ctdb, argc, argv);
2386         if (ret != 0) {
2387                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2388                 return ret;
2389         }
2390
2391         return 0;
2392 }
2393
2394 /*
2395   enable a disabled remote node
2396  */
2397 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2398 {
2399         int ret;
2400
2401         struct ctdb_node_map *nodemap=NULL;
2402
2403
2404         /* check if the node is already enabled */
2405         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2406                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2407                 exit(10);
2408         }
2409         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2410                 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2411                 return 0;
2412         }
2413
2414         do {
2415                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2416                 if (ret != 0) {
2417                         DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2418                         return ret;
2419                 }
2420
2421                 sleep(1);
2422
2423                 /* read the nodemap and verify the change took effect */
2424                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2425                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2426                         exit(10);
2427                 }
2428
2429         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2430
2431         ret = control_ipreallocate(ctdb, argc, argv);
2432         if (ret != 0) {
2433                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2434                 return ret;
2435         }
2436
2437         return 0;
2438 }
2439
2440 /*
2441   stop a remote node
2442  */
2443 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2444 {
2445         int ret;
2446         struct ctdb_node_map *nodemap=NULL;
2447
2448         do {
2449                 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2450                 if (ret != 0) {
2451                         DEBUG(DEBUG_ERR, ("Unable to stop node %u   try again\n", options.pnn));
2452                 }
2453         
2454                 sleep(1);
2455
2456                 /* read the nodemap and verify the change took effect */
2457                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2458                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2459                         exit(10);
2460                 }
2461
2462         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2463         ret = control_ipreallocate(ctdb, argc, argv);
2464         if (ret != 0) {
2465                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2466                 return ret;
2467         }
2468
2469         return 0;
2470 }
2471
2472 /*
2473   restart a stopped remote node
2474  */
2475 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2476 {
2477         int ret;
2478
2479         struct ctdb_node_map *nodemap=NULL;
2480
2481         do {
2482                 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2483                 if (ret != 0) {
2484                         DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2485                         return ret;
2486                 }
2487         
2488                 sleep(1);
2489
2490                 /* read the nodemap and verify the change took effect */
2491                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2492                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2493                         exit(10);
2494                 }
2495
2496         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2497         ret = control_ipreallocate(ctdb, argc, argv);
2498         if (ret != 0) {
2499                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2500                 return ret;
2501         }
2502
2503         return 0;
2504 }
2505
2506 static uint32_t get_generation(struct ctdb_context *ctdb)
2507 {
2508         struct ctdb_vnn_map *vnnmap=NULL;
2509         int ret;
2510
2511         /* wait until the recmaster is not in recovery mode */
2512         while (1) {
2513                 uint32_t recmode, recmaster;
2514                 
2515                 if (vnnmap != NULL) {
2516                         talloc_free(vnnmap);
2517                         vnnmap = NULL;
2518                 }
2519
2520                 /* get the recmaster */
2521                 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2522                 if (ret != 0) {
2523                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2524                         exit(10);
2525                 }
2526
2527                 /* get recovery mode */
2528                 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2529                 if (ret != 0) {
2530                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2531                         exit(10);
2532                 }
2533
2534                 /* get the current generation number */
2535                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2536                 if (ret != 0) {
2537                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2538                         exit(10);
2539                 }
2540
2541                 if ((recmode == CTDB_RECOVERY_NORMAL)
2542                 &&  (vnnmap->generation != 1)){
2543                         return vnnmap->generation;
2544                 }
2545                 sleep(1);
2546         }
2547 }
2548
2549 /*
2550   ban a node from the cluster
2551  */
2552 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2553 {
2554         int ret;
2555         struct ctdb_node_map *nodemap=NULL;
2556         struct ctdb_ban_time bantime;
2557
2558         if (argc < 1) {
2559                 usage();
2560         }
2561         
2562         /* verify the node exists */
2563         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2564         if (ret != 0) {
2565                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2566                 return ret;
2567         }
2568
2569         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2570                 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2571                 return -1;
2572         }
2573
2574         bantime.pnn  = options.pnn;
2575         bantime.time = strtoul(argv[0], NULL, 0);
2576
2577         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2578         if (ret != 0) {
2579                 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2580                 return -1;
2581         }       
2582
2583         ret = control_ipreallocate(ctdb, argc, argv);
2584         if (ret != 0) {
2585                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2586                 return ret;
2587         }
2588
2589         return 0;
2590 }
2591
2592
2593 /*
2594   unban a node from the cluster
2595  */
2596 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2597 {
2598         int ret;
2599         struct ctdb_node_map *nodemap=NULL;
2600         struct ctdb_ban_time bantime;
2601
2602         /* verify the node exists */
2603         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2604         if (ret != 0) {
2605                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2606                 return ret;
2607         }
2608
2609         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2610                 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2611                 return -1;
2612         }
2613
2614         bantime.pnn  = options.pnn;
2615         bantime.time = 0;
2616
2617         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2618         if (ret != 0) {
2619                 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2620                 return -1;
2621         }       
2622
2623         ret = control_ipreallocate(ctdb, argc, argv);
2624         if (ret != 0) {
2625                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2626                 return ret;
2627         }
2628
2629         return 0;
2630 }
2631
2632
2633 /*
2634   show ban information for a node
2635  */
2636 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2637 {
2638         int ret;
2639         struct ctdb_node_map *nodemap=NULL;
2640         struct ctdb_ban_time *bantime;
2641
2642         /* verify the node exists */
2643         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2644         if (ret != 0) {
2645                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2646                 return ret;
2647         }
2648
2649         ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2650         if (ret != 0) {
2651                 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2652                 return -1;
2653         }       
2654
2655         if (bantime->time == 0) {
2656                 printf("Node %u is not banned\n", bantime->pnn);
2657         } else {
2658                 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2659         }
2660
2661         return 0;
2662 }
2663
2664 /*
2665   shutdown a daemon
2666  */
2667 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2668 {
2669         int ret;
2670
2671         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2672         if (ret != 0) {
2673                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2674                 return ret;
2675         }
2676
2677         return 0;
2678 }
2679
2680 /*
2681   trigger a recovery
2682  */
2683 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2684 {
2685         int ret;
2686         uint32_t generation, next_generation;
2687
2688         /* record the current generation number */
2689         generation = get_generation(ctdb);
2690
2691         ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2692         if (ret != 0) {
2693                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2694                 return ret;
2695         }
2696
2697         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2698         if (ret != 0) {
2699                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2700                 return ret;
2701         }
2702
2703         /* wait until we are in a new generation */
2704         while (1) {
2705                 next_generation = get_generation(ctdb);
2706                 if (next_generation != generation) {
2707                         return 0;
2708                 }
2709                 sleep(1);
2710         }
2711
2712         return 0;
2713 }
2714
2715
2716 /*
2717   display monitoring mode of a remote node
2718  */
2719 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2720 {
2721         uint32_t monmode;
2722         int ret;
2723
2724         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2725         if (ret != 0) {
2726                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2727                 return ret;
2728         }
2729         if (!options.machinereadable){
2730                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2731         } else {
2732                 printf(":mode:\n");
2733                 printf(":%d:\n",monmode);
2734         }
2735         return 0;
2736 }
2737
2738
2739 /*
2740   display capabilities of a remote node
2741  */
2742 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2743 {
2744         uint32_t capabilities;
2745         int ret;
2746
2747         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2748         if (ret != 0) {
2749                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2750                 return ret;
2751         }
2752         
2753         if (!options.machinereadable){
2754                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2755                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2756                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2757                 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2758         } else {
2759                 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2760                 printf(":%d:%d:%d:%d:\n",
2761                         !!(capabilities&CTDB_CAP_RECMASTER),
2762                         !!(capabilities&CTDB_CAP_LMASTER),
2763                         !!(capabilities&CTDB_CAP_LVS),
2764                         !!(capabilities&CTDB_CAP_NATGW));
2765         }
2766         return 0;
2767 }
2768
2769 /*
2770   display lvs configuration
2771  */
2772 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2773 {
2774         uint32_t *capabilities;
2775         struct ctdb_node_map *nodemap=NULL;
2776         int i, ret;
2777         int healthy_count = 0;
2778
2779         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2780         if (ret != 0) {
2781                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2782                 return ret;
2783         }
2784
2785         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2786         CTDB_NO_MEMORY(ctdb, capabilities);
2787         
2788         /* collect capabilities for all connected nodes */
2789         for (i=0; i<nodemap->num; i++) {
2790                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2791                         continue;
2792                 }
2793                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2794                         continue;
2795                 }
2796         
2797                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2798                 if (ret != 0) {
2799                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2800                         return ret;
2801                 }
2802
2803                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2804                         continue;
2805                 }
2806
2807                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2808                         healthy_count++;
2809                 }
2810         }
2811
2812         /* Print all LVS nodes */
2813         for (i=0; i<nodemap->num; i++) {
2814                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2815                         continue;
2816                 }
2817                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2818                         continue;
2819                 }
2820                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2821                         continue;
2822                 }
2823
2824                 if (healthy_count != 0) {
2825                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2826                                 continue;
2827                         }
2828                 }
2829
2830                 printf("%d:%s\n", i, 
2831                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
2832         }
2833
2834         return 0;
2835 }
2836
2837 /*
2838   display who is the lvs master
2839  */
2840 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2841 {
2842         uint32_t *capabilities;
2843         struct ctdb_node_map *nodemap=NULL;
2844         int i, ret;
2845         int healthy_count = 0;
2846
2847         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2848         if (ret != 0) {
2849                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2850                 return ret;
2851         }
2852
2853         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2854         CTDB_NO_MEMORY(ctdb, capabilities);
2855         
2856         /* collect capabilities for all connected nodes */
2857         for (i=0; i<nodemap->num; i++) {
2858                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2859                         continue;
2860                 }
2861                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2862                         continue;
2863                 }
2864         
2865                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2866                 if (ret != 0) {
2867                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2868                         return ret;
2869                 }
2870
2871                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2872                         continue;
2873                 }
2874
2875                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2876                         healthy_count++;
2877                 }
2878         }
2879
2880         /* find and show the lvsmaster */
2881         for (i=0; i<nodemap->num; i++) {
2882                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2883                         continue;
2884                 }
2885                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2886                         continue;
2887                 }
2888                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2889                         continue;
2890                 }
2891
2892                 if (healthy_count != 0) {
2893                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2894                                 continue;
2895                         }
2896                 }
2897
2898                 if (options.machinereadable){
2899                         printf("%d\n", i);
2900                 } else {
2901                         printf("Node %d is LVS master\n", i);
2902                 }
2903                 return 0;
2904         }
2905
2906         printf("There is no LVS master\n");
2907         return -1;
2908 }
2909
2910 /*
2911   disable monitoring on a  node
2912  */
2913 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2914 {
2915         
2916         int ret;
2917
2918         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2919         if (ret != 0) {
2920                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2921                 return ret;
2922         }
2923         printf("Monitoring mode:%s\n","DISABLED");
2924
2925         return 0;
2926 }
2927
2928 /*
2929   enable monitoring on a  node
2930  */
2931 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2932 {
2933         
2934         int ret;
2935
2936         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2937         if (ret != 0) {
2938                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2939                 return ret;
2940         }
2941         printf("Monitoring mode:%s\n","ACTIVE");
2942
2943         return 0;
2944 }
2945
2946 /*
2947   display remote list of keys/data for a db
2948  */
2949 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2950 {
2951         const char *db_name;
2952         struct ctdb_db_context *ctdb_db;
2953         int ret;
2954
2955         if (argc < 1) {
2956                 usage();
2957         }
2958
2959         db_name = argv[0];
2960
2961
2962         if (db_exists(ctdb, db_name)) {
2963                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2964                 return -1;
2965         }
2966
2967         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2968
2969         if (ctdb_db == NULL) {
2970                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2971                 return -1;
2972         }
2973
2974         /* traverse and dump the cluster tdb */
2975         ret = ctdb_dump_db(ctdb_db, stdout);
2976         if (ret == -1) {
2977                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2978                 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
2979                                   " and 'ctdb getvar AllowUnhealthyDBRead'\n",
2980                                   db_name));
2981                 return -1;
2982         }
2983         talloc_free(ctdb_db);
2984
2985         printf("Dumped %d records\n", ret);
2986         return 0;
2987 }
2988
2989 /*
2990   display the content of a database key
2991  */
2992 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
2993 {
2994         const char *db_name;
2995         struct ctdb_db_context *ctdb_db;
2996         struct ctdb_record_handle *h;
2997         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2998         TDB_DATA key, data;
2999
3000         if (argc < 2) {
3001                 usage();
3002         }
3003
3004         db_name = argv[0];
3005
3006
3007         if (db_exists(ctdb, db_name)) {
3008                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3009                 return -1;
3010         }
3011
3012         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3013
3014         if (ctdb_db == NULL) {
3015                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3016                 return -1;
3017         }
3018
3019         key.dptr  = discard_const(argv[1]);
3020         key.dsize = strlen((char *)key.dptr);
3021  
3022         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3023         if (h == NULL) {
3024                 printf("Failed to fetch record '%s' on node %d\n", 
3025                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3026                 talloc_free(tmp_ctx);
3027                 exit(10);
3028         }
3029
3030         printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3031
3032         talloc_free(ctdb_db);
3033         talloc_free(tmp_ctx);
3034         return 0;
3035 }
3036
3037 /*
3038   display the content of a database key
3039  */
3040 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3041 {
3042         const char *db_name;
3043         struct ctdb_db_context *ctdb_db;
3044         struct ctdb_record_handle *h;
3045         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3046         TDB_DATA key, data;
3047
3048         if (argc < 3) {
3049                 usage();
3050         }
3051
3052         db_name = argv[0];
3053
3054
3055         if (db_exists(ctdb, db_name)) {
3056                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3057                 return -1;
3058         }
3059
3060         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3061
3062         if (ctdb_db == NULL) {
3063                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3064                 return -1;
3065         }
3066
3067         key.dptr  = discard_const(argv[1]);
3068         key.dsize = strlen((char *)key.dptr);
3069  
3070         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3071         if (h == NULL) {
3072                 printf("Failed to fetch record '%s' on node %d\n", 
3073                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3074                 talloc_free(tmp_ctx);
3075                 exit(10);
3076         }
3077
3078         data.dptr  = discard_const(argv[2]);
3079         data.dsize = strlen((char *)data.dptr);
3080  
3081         if (ctdb_record_store(h, data) != 0) {
3082                 printf("Failed to store record\n");
3083         }
3084
3085         talloc_free(h);
3086         talloc_free(ctdb_db);
3087         talloc_free(tmp_ctx);
3088         return 0;
3089 }
3090
3091 /*
3092   fetch a record from a persistent database
3093  */
3094 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3095 {
3096         const char *db_name;
3097         struct ctdb_db_context *ctdb_db;
3098         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3099         struct ctdb_transaction_handle *h;
3100         TDB_DATA key, data;
3101         int fd, ret;
3102
3103         if (argc < 2) {
3104                 talloc_free(tmp_ctx);
3105                 usage();
3106         }
3107
3108         db_name = argv[0];
3109
3110
3111         if (db_exists(ctdb, db_name)) {
3112                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3113                 talloc_free(tmp_ctx);
3114                 return -1;
3115         }
3116
3117         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3118
3119         if (ctdb_db == NULL) {
3120                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3121                 talloc_free(tmp_ctx);
3122                 return -1;
3123         }
3124
3125         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3126         if (h == NULL) {
3127                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3128                 talloc_free(tmp_ctx);
3129                 return -1;
3130         }
3131
3132         key.dptr  = discard_const(argv[1]);
3133         key.dsize = strlen(argv[1]);
3134         ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3135         if (ret != 0) {
3136                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3137                 talloc_free(tmp_ctx);
3138                 return -1;
3139         }
3140
3141         if (data.dsize == 0 || data.dptr == NULL) {
3142                 DEBUG(DEBUG_ERR,("Record is empty\n"));
3143                 talloc_free(tmp_ctx);
3144                 return -1;
3145         }
3146
3147         if (argc == 3) {
3148           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3149                 if (fd == -1) {
3150                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3151                         talloc_free(tmp_ctx);
3152                         return -1;
3153                 }
3154                 write(fd, data.dptr, data.dsize);
3155                 close(fd);
3156         } else {
3157                 write(1, data.dptr, data.dsize);
3158         }
3159
3160         /* abort the transaction */
3161         talloc_free(h);
3162
3163
3164         talloc_free(tmp_ctx);
3165         return 0;
3166 }
3167
3168 /*
3169   fetch a record from a tdb-file
3170  */
3171 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3172 {
3173         const char *tdb_file;
3174         TDB_CONTEXT *tdb;
3175         TDB_DATA key, data;
3176         int fd;
3177
3178         if (argc < 2) {
3179                 usage();
3180         }
3181
3182         tdb_file = argv[0];
3183
3184         tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3185         if (tdb == NULL) {
3186                 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3187                 return -1;
3188         }
3189
3190         key.dptr  = discard_const(argv[1]);
3191         key.dsize = strlen(argv[1]);
3192         data = tdb_fetch(tdb, key);
3193         if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3194                 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3195                 tdb_close(tdb);
3196                 return -1;
3197         }
3198
3199         tdb_close(tdb);
3200
3201         if (argc == 3) {
3202           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3203                 if (fd == -1) {
3204                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3205                         return -1;
3206                 }
3207                 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3208                 close(fd);
3209         } else {
3210                 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3211         }
3212
3213         return 0;
3214 }
3215
3216 /*
3217   write a record to a persistent database
3218  */
3219 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3220 {
3221         const char *db_name;
3222         struct ctdb_db_context *ctdb_db;
3223         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3224         struct ctdb_transaction_handle *h;
3225         struct stat st;
3226         TDB_DATA key, data;
3227         int fd, ret;
3228
3229         if (argc < 3) {
3230                 talloc_free(tmp_ctx);
3231                 usage();
3232         }
3233
3234         fd = open(argv[2], O_RDONLY);
3235         if (fd == -1) {
3236                 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s  %s\n", argv[2], strerror(errno)));
3237                 talloc_free(tmp_ctx);
3238                 return -1;
3239         }
3240         
3241         ret = fstat(fd, &st);
3242         if (ret == -1) {
3243                 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3244                 close(fd);
3245                 talloc_free(tmp_ctx);
3246                 return -1;
3247         }
3248
3249         if (!S_ISREG(st.st_mode)) {
3250                 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3251                 close(fd);
3252                 talloc_free(tmp_ctx);
3253                 return -1;
3254         }
3255
3256         data.dsize = st.st_size;
3257         if (data.dsize == 0) {
3258                 data.dptr  = NULL;
3259         } else {
3260                 data.dptr = talloc_size(tmp_ctx, data.dsize);
3261                 if (data.dptr == NULL) {
3262                         DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3263                         close(fd);
3264                         talloc_free(tmp_ctx);
3265                         return -1;
3266                 }
3267                 ret = read(fd, data.dptr, data.dsize);
3268                 if (ret != data.dsize) {
3269                         DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3270                         close(fd);
3271                         talloc_free(tmp_ctx);
3272                         return -1;
3273                 }
3274         }
3275         close(fd);
3276
3277
3278         db_name = argv[0];
3279
3280         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3281
3282         if (ctdb_db == NULL) {
3283                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3284                 talloc_free(tmp_ctx);
3285                 return -1;
3286         }
3287
3288         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3289         if (h == NULL) {
3290                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3291                 talloc_free(tmp_ctx);
3292                 return -1;
3293         }
3294
3295         key.dptr  = discard_const(argv[1]);
3296         key.dsize = strlen(argv[1]);
3297         ret = ctdb_transaction_store(h, key, data);
3298         if (ret != 0) {
3299                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3300                 talloc_free(tmp_ctx);
3301                 return -1;
3302         }
3303
3304         ret = ctdb_transaction_commit(h);
3305         if (ret != 0) {
3306                 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3307                 talloc_free(tmp_ctx);
3308                 return -1;
3309         }
3310
3311
3312         talloc_free(tmp_ctx);
3313         return 0;
3314 }
3315
3316 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid, 
3317                              TDB_DATA data, void *private_data)
3318 {
3319         DEBUG(DEBUG_ERR,("Log data received\n"));
3320         if (data.dsize > 0) {
3321                 printf("%s", data.dptr);
3322         }
3323
3324         exit(0);
3325 }
3326
3327 /*
3328   display a list of log messages from the in memory ringbuffer
3329  */
3330 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3331 {
3332         int ret;
3333         int32_t res;
3334         struct ctdb_get_log_addr log_addr;
3335         TDB_DATA data;
3336         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3337         char *errmsg;
3338         struct timeval tv;
3339
3340         if (argc != 1) {
3341                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3342                 talloc_free(tmp_ctx);
3343                 return -1;
3344         }
3345
3346         log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3347         log_addr.srvid = getpid();
3348         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3349                 log_addr.level = get_debug_by_desc(argv[0]);
3350         } else {
3351                 log_addr.level = strtol(argv[0], NULL, 0);
3352         }
3353
3354
3355         data.dptr = (unsigned char *)&log_addr;
3356         data.dsize = sizeof(log_addr);
3357
3358         DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3359
3360         ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3361         sleep(1);
3362
3363         DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3364
3365         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3366                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3367         if (ret != 0 || res != 0) {
3368                 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3369                 talloc_free(tmp_ctx);
3370                 return -1;
3371         }
3372
3373
3374         tv = timeval_current();
3375         /* this loop will terminate when we have received the reply */
3376         while (timeval_elapsed(&tv) < 3.0) {    
3377                 event_loop_once(ctdb->ev);
3378         }
3379
3380         DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3381
3382         talloc_free(tmp_ctx);
3383         return 0;
3384 }
3385
3386 /*
3387   clear the in memory log area
3388  */
3389 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3390 {
3391         int ret;
3392         int32_t res;
3393         char *errmsg;
3394         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3395
3396         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3397                            0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3398         if (ret != 0 || res != 0) {
3399                 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3400                 talloc_free(tmp_ctx);
3401                 return -1;
3402         }
3403
3404         talloc_free(tmp_ctx);
3405         return 0;
3406 }
3407
3408
3409
3410 /*
3411   display a list of the databases on a remote ctdb
3412  */
3413 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3414 {
3415         int i, ret;
3416         struct ctdb_dbid_map *dbmap=NULL;
3417
3418         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3419         if (ret != 0) {
3420                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3421                 return ret;
3422         }
3423
3424         if(options.machinereadable){
3425                 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3426                 for(i=0;i<dbmap->num;i++){
3427                         const char *path;
3428                         const char *name;
3429                         const char *health;
3430                         bool persistent;
3431
3432                         ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3433                                             dbmap->dbs[i].dbid, ctdb, &path);
3434                         ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3435                                             dbmap->dbs[i].dbid, ctdb, &name);
3436                         ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3437                                               dbmap->dbs[i].dbid, ctdb, &health);
3438                         persistent = dbmap->dbs[i].persistent;
3439                         printf(":0x%08X:%s:%s:%d:%d:\n",
3440                                dbmap->dbs[i].dbid, name, path,
3441                                !!(persistent), !!(health));
3442                 }
3443                 return 0;
3444         }
3445
3446         printf("Number of databases:%d\n", dbmap->num);
3447         for(i=0;i<dbmap->num;i++){
3448                 const char *path;
3449                 const char *name;
3450                 const char *health;
3451                 bool persistent;
3452
3453                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3454                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3455                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3456                 persistent = dbmap->dbs[i].persistent;
3457                 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3458                        dbmap->dbs[i].dbid, name, path,
3459                        persistent?" PERSISTENT":"",
3460                        health?" UNHEALTHY":"");
3461         }
3462
3463         return 0;
3464 }
3465
3466 /*
3467   display the status of a database on a remote ctdb
3468  */
3469 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3470 {
3471         int i, ret;
3472         struct ctdb_dbid_map *dbmap=NULL;
3473         const char *db_name;
3474
3475         if (argc < 1) {
3476                 usage();
3477         }
3478
3479         db_name = argv[0];
3480
3481         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3482         if (ret != 0) {
3483                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3484                 return ret;
3485         }
3486
3487         for(i=0;i<dbmap->num;i++){
3488                 const char *path;
3489                 const char *name;
3490                 const char *health;
3491                 bool persistent;
3492
3493                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3494                 if (strcmp(name, db_name) != 0) {
3495                         continue;
3496                 }
3497
3498                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3499                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3500                 persistent = dbmap->dbs[i].persistent;
3501                 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3502                        dbmap->dbs[i].dbid, name, path,
3503                        persistent?"yes":"no",
3504                        health?health:"OK");
3505                 return 0;
3506         }
3507
3508         DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3509         return 0;
3510 }
3511
3512 /*
3513   check if the local node is recmaster or not
3514   it will return 1 if this node is the recmaster and 0 if it is not
3515   or if the local ctdb daemon could not be contacted
3516  */
3517 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3518 {
3519         uint32_t mypnn, recmaster;
3520         int ret;
3521
3522         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3523         if (mypnn == -1) {
3524                 printf("Failed to get pnn of node\n");
3525                 return 1;
3526         }
3527
3528         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3529         if (ret != 0) {
3530                 printf("Failed to get the recmaster\n");
3531                 return 1;
3532         }
3533
3534         if (recmaster != mypnn) {
3535                 printf("this node is not the recmaster\n");
3536                 return 1;
3537         }
3538
3539         printf("this node is the recmaster\n");
3540         return 0;
3541 }
3542
3543 /*
3544   ping a node
3545  */
3546 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3547 {
3548         int ret;
3549         struct timeval tv = timeval_current();
3550         ret = ctdb_ctrl_ping(ctdb, options.pnn);
3551         if (ret == -1) {
3552                 printf("Unable to get ping response from node %u\n", options.pnn);
3553                 return -1;
3554         } else {
3555                 printf("response from %u time=%.6f sec  (%d clients)\n", 
3556                        options.pnn, timeval_elapsed(&tv), ret);
3557         }
3558         return 0;
3559 }
3560
3561
3562 /*
3563   get a tunable
3564  */
3565 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3566 {
3567         const char *name;
3568         uint32_t value;
3569         int ret;
3570
3571         if (argc < 1) {
3572                 usage();
3573         }
3574
3575         name = argv[0];
3576         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3577         if (ret == -1) {
3578                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3579                 return -1;
3580         }
3581
3582         printf("%-19s = %u\n", name, value);
3583         return 0;
3584 }
3585
3586 /*
3587   set a tunable
3588  */
3589 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3590 {
3591         const char *name;
3592         uint32_t value;
3593         int ret;
3594
3595         if (argc < 2) {
3596                 usage();
3597         }
3598
3599         name = argv[0];
3600         value = strtoul(argv[1], NULL, 0);
3601
3602         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3603         if (ret == -1) {
3604                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3605                 return -1;
3606         }
3607         return 0;
3608 }
3609
3610 /*
3611   list all tunables
3612  */
3613 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3614 {
3615         uint32_t count;
3616         const char **list;
3617         int ret, i;
3618
3619         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3620         if (ret == -1) {
3621                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3622                 return -1;
3623         }
3624
3625         for (i=0;i<count;i++) {
3626                 control_getvar(ctdb, 1, &list[i]);
3627         }
3628
3629         talloc_free(list);
3630         
3631         return 0;
3632 }
3633
3634 /*
3635   display debug level on a node
3636  */
3637 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3638 {
3639         int ret;
3640         int32_t level;
3641
3642         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3643         if (ret != 0) {
3644                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3645                 return ret;
3646         } else {
3647                 if (options.machinereadable){
3648                         printf(":Name:Level:\n");
3649                         printf(":%s:%d:\n",get_debug_by_level(level),level);
3650                 } else {
3651                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3652                 }
3653         }
3654         return 0;
3655 }
3656
3657 /*
3658   display reclock file of a node
3659  */
3660 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3661 {
3662         int ret;
3663         const char *reclock;
3664
3665         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3666         if (ret != 0) {
3667                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3668                 return ret;
3669         } else {
3670                 if (options.machinereadable){
3671                         if (reclock != NULL) {
3672                                 printf("%s", reclock);
3673                         }
3674                 } else {
3675                         if (reclock == NULL) {
3676                                 printf("No reclock file used.\n");
3677                         } else {
3678                                 printf("Reclock file:%s\n", reclock);
3679                         }
3680                 }
3681         }
3682         return 0;
3683 }
3684
3685 /*
3686   set the reclock file of a node
3687  */
3688 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3689 {
3690         int ret;
3691         const char *reclock;
3692
3693         if (argc == 0) {
3694                 reclock = NULL;
3695         } else if (argc == 1) {
3696                 reclock = argv[0];
3697         } else {
3698                 usage();
3699         }
3700
3701         ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3702         if (ret != 0) {
3703                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3704                 return ret;
3705         }
3706         return 0;
3707 }
3708
3709 /*
3710   set the natgw state on/off
3711  */
3712 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3713 {
3714         int ret;
3715         uint32_t natgwstate;
3716
3717         if (argc == 0) {
3718                 usage();
3719         }
3720
3721         if (!strcmp(argv[0], "on")) {
3722                 natgwstate = 1;
3723         } else if (!strcmp(argv[0], "off")) {
3724                 natgwstate = 0;
3725         } else {
3726                 usage();
3727         }
3728
3729         ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3730         if (ret != 0) {
3731                 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3732                 return ret;
3733         }
3734
3735         return 0;
3736 }
3737
3738 /*
3739   set the lmaster role on/off
3740  */
3741 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3742 {
3743         int ret;
3744         uint32_t lmasterrole;
3745
3746         if (argc == 0) {
3747                 usage();
3748         }
3749
3750         if (!strcmp(argv[0], "on")) {
3751                 lmasterrole = 1;
3752         } else if (!strcmp(argv[0], "off")) {
3753                 lmasterrole = 0;
3754         } else {
3755                 usage();
3756         }
3757
3758         ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3759         if (ret != 0) {
3760                 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3761                 return ret;
3762         }
3763
3764         return 0;
3765 }
3766
3767 /*
3768   set the recmaster role on/off
3769  */
3770 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3771 {
3772         int ret;
3773         uint32_t recmasterrole;
3774
3775         if (argc == 0) {
3776                 usage();
3777         }
3778
3779         if (!strcmp(argv[0], "on")) {
3780                 recmasterrole = 1;
3781         } else if (!strcmp(argv[0], "off")) {
3782                 recmasterrole = 0;
3783         } else {
3784                 usage();
3785         }
3786
3787         ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3788         if (ret != 0) {
3789                 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3790                 return ret;
3791         }
3792
3793         return 0;
3794 }
3795
3796 /*
3797   set debug level on a node or all nodes
3798  */
3799 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3800 {
3801         int i, ret;
3802         int32_t level;
3803
3804         if (argc == 0) {
3805                 printf("You must specify the debug level. Valid levels are:\n");
3806                 for (i=0; debug_levels[i].description != NULL; i++) {
3807                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3808                 }
3809
3810                 return 0;
3811         }
3812
3813         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3814                 level = get_debug_by_desc(argv[0]);
3815         } else {
3816                 level = strtol(argv[0], NULL, 0);
3817         }
3818
3819         for (i=0; debug_levels[i].description != NULL; i++) {
3820                 if (level == debug_levels[i].level) {
3821                         break;
3822                 }
3823         }
3824         if (debug_levels[i].description == NULL) {
3825                 printf("Invalid debug level, must be one of\n");
3826                 for (i=0; debug_levels[i].description != NULL; i++) {
3827                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3828                 }
3829                 return -1;
3830         }
3831
3832         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3833         if (ret != 0) {
3834                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3835         }
3836         return 0;
3837 }
3838
3839
3840 /*
3841   thaw a node
3842  */
3843 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3844 {
3845         int ret;
3846         uint32_t priority;
3847         
3848         if (argc == 1) {
3849                 priority = strtol(argv[0], NULL, 0);
3850         } else {
3851                 priority = 0;
3852         }
3853         DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3854
3855         ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3856         if (ret != 0) {
3857                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3858         }               
3859         return 0;
3860 }
3861
3862
3863 /*
3864   attach to a database
3865  */
3866 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3867 {
3868         const char *db_name;
3869         struct ctdb_db_context *ctdb_db;
3870
3871         if (argc < 1) {
3872                 usage();
3873         }
3874         db_name = argv[0];
3875
3876         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3877         if (ctdb_db == NULL) {
3878                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3879                 return -1;
3880         }
3881
3882         return 0;
3883 }
3884
3885 /*
3886   set db priority
3887  */
3888 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3889 {
3890         struct ctdb_db_priority db_prio;
3891         int ret;
3892
3893         if (argc < 2) {
3894                 usage();
3895         }
3896
3897         db_prio.db_id    = strtoul(argv[0], NULL, 0);
3898         db_prio.priority = strtoul(argv[1], NULL, 0);
3899
3900         ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3901         if (ret != 0) {
3902                 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3903                 return -1;
3904         }
3905
3906         return 0;
3907 }
3908
3909 /*
3910   get db priority
3911  */
3912 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3913 {
3914         uint32_t db_id, priority;
3915         int ret;
3916
3917         if (argc < 1) {
3918                 usage();
3919         }
3920
3921         db_id = strtoul(argv[0], NULL, 0);
3922
3923         ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
3924         if (ret != 0) {
3925                 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
3926                 return -1;
3927         }
3928
3929         DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
3930
3931         return 0;
3932 }
3933
3934 /*
3935   run an eventscript on a node
3936  */
3937 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
3938 {
3939         TDB_DATA data;
3940         int ret;
3941         int32_t res;
3942         char *errmsg;
3943         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3944
3945         if (argc != 1) {
3946                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3947                 return -1;
3948         }
3949
3950         data.dptr = (unsigned char *)discard_const(argv[0]);
3951         data.dsize = strlen((char *)data.dptr) + 1;
3952
3953         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
3954
3955         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
3956                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3957         if (ret != 0 || res != 0) {
3958                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
3959                 talloc_free(tmp_ctx);
3960                 return -1;
3961         }
3962         talloc_free(tmp_ctx);
3963         return 0;
3964 }
3965
3966 #define DB_VERSION 1
3967 #define MAX_DB_NAME 64
3968 struct db_file_header {
3969         unsigned long version;
3970         time_t timestamp;
3971         unsigned long persistent;
3972         unsigned long size;
3973         const char name[MAX_DB_NAME];
3974 };
3975
3976 struct backup_data {
3977         struct ctdb_marshall_buffer *records;
3978         uint32_t len;
3979         uint32_t total;
3980         bool traverse_error;
3981 };
3982
3983 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
3984 {
3985         struct backup_data *bd = talloc_get_type(private, struct backup_data);
3986         struct ctdb_rec_data *rec;
3987
3988         /* add the record */
3989         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
3990         if (rec == NULL) {
3991                 bd->traverse_error = true;
3992                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
3993                 return -1;
3994         }
3995         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
3996         if (bd->records == NULL) {
3997                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
3998                 bd->traverse_error = true;
3999                 return -1;
4000         }
4001         bd->records->count++;
4002         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
4003         bd->len += rec->length;
4004         talloc_free(rec);
4005
4006         bd->total++;
4007         return 0;
4008 }
4009
4010 /*
4011  * backup a database to a file 
4012  */
4013 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
4014 {
4015         int i, ret;
4016         struct ctdb_dbid_map *dbmap=NULL;
4017         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4018         struct db_file_header dbhdr;
4019         struct ctdb_db_context *ctdb_db;
4020         struct backup_data *bd;
4021         int fh = -1;
4022         int status = -1;
4023         const char *reason = NULL;
4024
4025         if (argc != 2) {
4026                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4027                 return -1;
4028         }
4029
4030         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4031         if (ret != 0) {
4032                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4033                 return ret;
4034         }
4035
4036         for(i=0;i<dbmap->num;i++){
4037                 const char *name;
4038
4039                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4040                 if(!strcmp(argv[0], name)){
4041                         talloc_free(discard_const(name));
4042                         break;
4043                 }
4044                 talloc_free(discard_const(name));
4045         }
4046         if (i == dbmap->num) {
4047                 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4048                 talloc_free(tmp_ctx);
4049                 return -1;
4050         }
4051
4052         ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4053                                     dbmap->dbs[i].dbid, tmp_ctx, &reason);
4054         if (ret != 0) {
4055                 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4056                                  argv[0]));
4057                 talloc_free(tmp_ctx);
4058                 return -1;
4059         }
4060         if (reason) {
4061                 uint32_t allow_unhealthy = 0;
4062
4063                 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4064                                       "AllowUnhealthyDBRead",
4065                                       &allow_unhealthy);
4066
4067                 if (allow_unhealthy != 1) {
4068                         DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4069                                          argv[0], reason));
4070
4071                         DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
4072                                          allow_unhealthy));
4073                         talloc_free(tmp_ctx);
4074                         return -1;
4075                 }
4076
4077                 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4078                                      argv[0], argv[0]));
4079                 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4080                                      "tunnable AllowUnhealthyDBRead = %u\n",
4081                                      allow_unhealthy));
4082         }
4083
4084         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4085         if (ctdb_db == NULL) {
4086                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4087                 talloc_free(tmp_ctx);
4088                 return -1;
4089         }
4090
4091
4092         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4093         if (ret == -1) {
4094                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4095                 talloc_free(tmp_ctx);
4096                 return -1;
4097         }
4098
4099
4100         bd = talloc_zero(tmp_ctx, struct backup_data);
4101         if (bd == NULL) {
4102                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4103                 talloc_free(tmp_ctx);
4104                 return -1;
4105         }
4106
4107         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4108         if (bd->records == NULL) {
4109                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4110                 talloc_free(tmp_ctx);
4111                 return -1;
4112         }
4113
4114         bd->len = offsetof(struct ctdb_marshall_buffer, data);
4115         bd->records->db_id = ctdb_db->db_id;
4116         /* traverse the database collecting all records */
4117         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4118             bd->traverse_error) {
4119                 DEBUG(DEBUG_ERR,("Traverse error\n"));
4120                 talloc_free(tmp_ctx);
4121                 return -1;              
4122         }
4123
4124         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4125
4126
4127         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4128         if (fh == -1) {
4129                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4130                 talloc_free(tmp_ctx);
4131                 return -1;
4132         }
4133
4134         dbhdr.version = DB_VERSION;
4135         dbhdr.timestamp = time(NULL);
4136         dbhdr.persistent = dbmap->dbs[i].persistent;
4137         dbhdr.size = bd->len;
4138         if (strlen(argv[0]) >= MAX_DB_NAME) {
4139                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4140                 goto done;
4141         }
4142         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4143         ret = write(fh, &dbhdr, sizeof(dbhdr));
4144         if (ret == -1) {
4145                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4146                 goto done;
4147         }
4148         ret = write(fh, bd->records, bd->len);
4149         if (ret == -1) {
4150                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4151                 goto done;
4152         }
4153
4154         status = 0;
4155 done:
4156         if (fh != -1) {
4157                 ret = close(fh);
4158                 if (ret == -1) {
4159                         DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4160                 }
4161         }
4162         talloc_free(tmp_ctx);
4163         return status;
4164 }
4165
4166 /*
4167  * restore a database from a file 
4168  */
4169 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4170 {
4171         int ret;
4172         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4173         TDB_DATA outdata;
4174         TDB_DATA data;
4175         struct db_file_header dbhdr;
4176         struct ctdb_db_context *ctdb_db;
4177         struct ctdb_node_map *nodemap=NULL;
4178         struct ctdb_vnn_map *vnnmap=NULL;
4179         int i, fh;
4180         struct ctdb_control_wipe_database w;
4181         uint32_t *nodes;
4182         uint32_t generation;
4183         struct tm *tm;
4184         char tbuf[100];
4185         char *dbname;
4186
4187         if (argc < 1 || argc > 2) {
4188                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4189                 return -1;
4190         }
4191
4192         fh = open(argv[0], O_RDONLY);
4193         if (fh == -1) {
4194                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4195                 talloc_free(tmp_ctx);
4196                 return -1;
4197         }
4198
4199         read(fh, &dbhdr, sizeof(dbhdr));
4200         if (dbhdr.version != DB_VERSION) {
4201                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4202                 talloc_free(tmp_ctx);
4203                 return -1;
4204         }
4205
4206         dbname = discard_const(dbhdr.name);
4207         if (argc == 2) {
4208                 dbname = discard_const(argv[1]);
4209         }
4210
4211         outdata.dsize = dbhdr.size;
4212         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4213         if (outdata.dptr == NULL) {
4214                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4215                 close(fh);
4216                 talloc_free(tmp_ctx);
4217                 return -1;
4218         }               
4219         read(fh, outdata.dptr, outdata.dsize);
4220         close(fh);
4221
4222         tm = localtime(&dbhdr.timestamp);
4223         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4224         printf("Restoring database '%s' from backup @ %s\n",
4225                 dbname, tbuf);
4226
4227
4228         ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4229         if (ctdb_db == NULL) {
4230                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4231                 talloc_free(tmp_ctx);
4232                 return -1;
4233         }
4234
4235         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4236         if (ret != 0) {
4237                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4238                 talloc_free(tmp_ctx);
4239                 return ret;
4240         }
4241
4242
4243         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4244         if (ret != 0) {
4245                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4246                 talloc_free(tmp_ctx);
4247                 return ret;
4248         }
4249
4250         /* freeze all nodes */
4251         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4252         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4253                 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4254                                         nodes, i,
4255                                         TIMELIMIT(),
4256                                         false, tdb_null,
4257                                         NULL, NULL,
4258                                         NULL) != 0) {
4259                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4260                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4261                         talloc_free(tmp_ctx);
4262                         return -1;
4263                 }
4264         }
4265
4266         generation = vnnmap->generation;
4267         data.dptr = (void *)&generation;
4268         data.dsize = sizeof(generation);
4269
4270         /* start a cluster wide transaction */
4271         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4272         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4273                                         nodes, 0,
4274                                         TIMELIMIT(), false, data,
4275                                         NULL, NULL,
4276                                         NULL) != 0) {
4277                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4278                 return -1;
4279         }
4280
4281
4282         w.db_id = ctdb_db->db_id;
4283         w.transaction_id = generation;
4284
4285         data.dptr = (void *)&w;
4286         data.dsize = sizeof(w);
4287
4288         /* wipe all the remote databases. */
4289         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4290         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4291                                         nodes, 0,
4292                                         TIMELIMIT(), false, data,
4293                                         NULL, NULL,
4294                                         NULL) != 0) {
4295                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4296                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4297                 talloc_free(tmp_ctx);
4298                 return -1;
4299         }
4300         
4301         /* push the database */
4302         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4303         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4304                                         nodes, 0,
4305                                         TIMELIMIT(), false, outdata,
4306                                         NULL, NULL,
4307                                         NULL) != 0) {
4308                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4309                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4310                 talloc_free(tmp_ctx);
4311                 return -1;
4312         }
4313
4314         data.dptr = (void *)&ctdb_db->db_id;
4315         data.dsize = sizeof(ctdb_db->db_id);
4316
4317         /* mark the database as healthy */
4318         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4319         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4320                                         nodes, 0,
4321                                         TIMELIMIT(), false, data,
4322                                         NULL, NULL,
4323                                         NULL) != 0) {
4324                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4325                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4326                 talloc_free(tmp_ctx);
4327                 return -1;
4328         }
4329
4330         data.dptr = (void *)&generation;
4331         data.dsize = sizeof(generation);
4332
4333         /* commit all the changes */
4334         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4335                                         nodes, 0,
4336                                         TIMELIMIT(), false, data,
4337                                         NULL, NULL,
4338                                         NULL) != 0) {
4339                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4340                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4341                 talloc_free(tmp_ctx);
4342                 return -1;
4343         }
4344
4345
4346         /* thaw all nodes */
4347         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4348         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4349                                         nodes, 0,
4350                                         TIMELIMIT(),
4351                                         false, tdb_null,
4352                                         NULL, NULL,
4353                                         NULL) != 0) {
4354                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4355                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4356                 talloc_free(tmp_ctx);
4357                 return -1;
4358         }
4359
4360
4361         talloc_free(tmp_ctx);
4362         return 0;
4363 }
4364
4365 /*
4366  * dump a database backup from a file
4367  */
4368 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4369 {
4370         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4371         TDB_DATA outdata;
4372         struct db_file_header dbhdr;
4373         int i, fh;
4374         struct tm *tm;
4375         char tbuf[100];
4376         struct ctdb_rec_data *rec = NULL;
4377         struct ctdb_marshall_buffer *m;
4378
4379         if (argc != 1) {
4380                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4381                 return -1;
4382         }
4383
4384         fh = open(argv[0], O_RDONLY);
4385         if (fh == -1) {
4386                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4387                 talloc_free(tmp_ctx);
4388                 return -1;
4389         }
4390
4391         read(fh, &dbhdr, sizeof(dbhdr));
4392         if (dbhdr.version != DB_VERSION) {
4393                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4394                 talloc_free(tmp_ctx);
4395                 return -1;
4396         }
4397
4398         outdata.dsize = dbhdr.size;
4399         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4400         if (outdata.dptr == NULL) {
4401                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4402                 close(fh);
4403                 talloc_free(tmp_ctx);
4404                 return -1;
4405         }
4406         read(fh, outdata.dptr, outdata.dsize);
4407         close(fh);
4408         m = (struct ctdb_marshall_buffer *)outdata.dptr;
4409
4410         tm = localtime(&dbhdr.timestamp);
4411         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4412         printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4413                 dbhdr.name, m->db_id, tbuf);
4414
4415         for (i=0; i < m->count; i++) {
4416                 uint32_t reqid = 0;
4417                 TDB_DATA key, data;
4418
4419                 /* we do not want the header splitted, so we pass NULL*/
4420                 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4421                                               NULL, &key, &data);
4422
4423                 ctdb_dumpdb_record(ctdb, key, data, stdout);
4424         }
4425
4426         printf("Dumped %d records\n", i);
4427         talloc_free(tmp_ctx);
4428         return 0;
4429 }
4430
4431 /*
4432  * wipe a database from a file
4433  */
4434 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4435                           const char **argv)
4436 {
4437         int ret;
4438         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4439         TDB_DATA data;
4440         struct ctdb_db_context *ctdb_db;
4441         struct ctdb_node_map *nodemap = NULL;
4442         struct ctdb_vnn_map *vnnmap = NULL;
4443         int i;
4444         struct ctdb_control_wipe_database w;
4445         uint32_t *nodes;
4446         uint32_t generation;
4447         struct ctdb_dbid_map *dbmap = NULL;
4448
4449         if (argc != 1) {
4450                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4451                 return -1;
4452         }
4453
4454         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4455                                  &dbmap);
4456         if (ret != 0) {
4457                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4458                                   options.pnn));
4459                 return ret;
4460         }
4461
4462         for(i=0;i<dbmap->num;i++){
4463                 const char *name;
4464
4465                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4466                                     dbmap->dbs[i].dbid, tmp_ctx, &name);
4467                 if(!strcmp(argv[0], name)){
4468                         talloc_free(discard_const(name));
4469                         break;
4470                 }
4471                 talloc_free(discard_const(name));
4472         }
4473         if (i == dbmap->num) {
4474                 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4475                                   argv[0]));
4476                 talloc_free(tmp_ctx);
4477                 return -1;
4478         }
4479
4480         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4481         if (ctdb_db == NULL) {
4482                 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4483                                   argv[0]));
4484                 talloc_free(tmp_ctx);
4485                 return -1;
4486         }
4487
4488         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4489                                    &nodemap);
4490         if (ret != 0) {
4491                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4492                                   options.pnn));
4493                 talloc_free(tmp_ctx);
4494                 return ret;
4495         }
4496
4497         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4498                                   &vnnmap);
4499         if (ret != 0) {
4500                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4501                                   options.pnn));
4502                 talloc_free(tmp_ctx);
4503                 return ret;
4504         }
4505
4506         /* freeze all nodes */
4507         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4508         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4509                 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4510                                                 nodes, i,
4511                                                 TIMELIMIT(),
4512                                                 false, tdb_null,
4513                                                 NULL, NULL,
4514                                                 NULL);
4515                 if (ret != 0) {
4516                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4517                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4518                                              CTDB_RECOVERY_ACTIVE);
4519                         talloc_free(tmp_ctx);
4520                         return -1;
4521                 }
4522         }
4523
4524         generation = vnnmap->generation;
4525         data.dptr = (void *)&generation;
4526         data.dsize = sizeof(generation);
4527
4528         /* start a cluster wide transaction */
4529         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4530         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4531                                         nodes, 0,
4532                                         TIMELIMIT(), false, data,
4533                                         NULL, NULL,
4534                                         NULL);
4535         if (ret!= 0) {
4536                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4537                                   "transactions.\n"));
4538                 return -1;
4539         }
4540
4541         w.db_id = ctdb_db->db_id;
4542         w.transaction_id = generation;
4543
4544         data.dptr = (void *)&w;
4545         data.dsize = sizeof(w);
4546
4547         /* wipe all the remote databases. */
4548         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4549         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4550                                         nodes, 0,
4551                                         TIMELIMIT(), false, data,
4552                                         NULL, NULL,
4553                                         NULL) != 0) {
4554                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4555                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4556                 talloc_free(tmp_ctx);
4557                 return -1;
4558         }
4559
4560         data.dptr = (void *)&ctdb_db->db_id;
4561         data.dsize = sizeof(ctdb_db->db_id);
4562
4563         /* mark the database as healthy */
4564         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4565         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4566                                         nodes, 0,
4567                                         TIMELIMIT(), false, data,
4568                                         NULL, NULL,
4569                                         NULL) != 0) {
4570                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4571                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4572                 talloc_free(tmp_ctx);
4573                 return -1;
4574         }
4575
4576         data.dptr = (void *)&generation;
4577         data.dsize = sizeof(generation);
4578
4579         /* commit all the changes */
4580         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4581                                         nodes, 0,
4582                                         TIMELIMIT(), false, data,
4583                                         NULL, NULL,
4584                                         NULL) != 0) {
4585                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4586                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4587                 talloc_free(tmp_ctx);
4588                 return -1;
4589         }
4590
4591         /* thaw all nodes */
4592         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4593         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4594                                         nodes, 0,
4595                                         TIMELIMIT(),
4596                                         false, tdb_null,
4597                                         NULL, NULL,
4598                                         NULL) != 0) {
4599                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4600                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4601                 talloc_free(tmp_ctx);
4602                 return -1;
4603         }
4604
4605         talloc_free(tmp_ctx);
4606         return 0;
4607 }
4608
4609 /*
4610   dump memory usage
4611  */
4612 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4613 {
4614         TDB_DATA data;
4615         int ret;
4616         int32_t res;
4617         char *errmsg;
4618         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4619         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4620                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4621         if (ret != 0 || res != 0) {
4622                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4623                 talloc_free(tmp_ctx);
4624                 return -1;
4625         }
4626         write(1, data.dptr, data.dsize);
4627         talloc_free(tmp_ctx);
4628         return 0;
4629 }
4630
4631 /*
4632   handler for memory dumps
4633 */
4634 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4635                              TDB_DATA data, void *private_data)
4636 {
4637         write(1, data.dptr, data.dsize);
4638         exit(0);
4639 }
4640
4641 /*
4642   dump memory usage on the recovery daemon
4643  */
4644 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4645 {
4646         int ret;
4647         TDB_DATA data;
4648         struct rd_memdump_reply rd;
4649
4650         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4651         if (rd.pnn == -1) {
4652                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4653                 return -1;
4654         }
4655         rd.srvid = getpid();
4656
4657         /* register a message port for receiveing the reply so that we
4658            can receive the reply
4659         */
4660         ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4661
4662
4663         data.dptr = (uint8_t *)&rd;
4664         data.dsize = sizeof(rd);
4665
4666         ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4667         if (ret != 0) {
4668                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4669                 return -1;
4670         }
4671
4672         /* this loop will terminate when we have received the reply */
4673         while (1) {     
4674                 event_loop_once(ctdb->ev);
4675         }
4676
4677         return 0;
4678 }
4679
4680 /*
4681   send a message to a srvid
4682  */
4683 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4684 {
4685         unsigned long srvid;
4686         int ret;
4687         TDB_DATA data;
4688
4689         if (argc < 2) {
4690                 usage();
4691         }
4692
4693         srvid      = strtoul(argv[0], NULL, 0);
4694
4695         data.dptr = (uint8_t *)discard_const(argv[1]);
4696         data.dsize= strlen(argv[1]);
4697
4698         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4699         if (ret != 0) {
4700                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4701                 return -1;
4702         }
4703
4704         return 0;
4705 }
4706
4707 /*
4708   handler for msglisten
4709 */
4710 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4711                              TDB_DATA data, void *private_data)
4712 {
4713         int i;
4714
4715         printf("Message received: ");
4716         for (i=0;i<data.dsize;i++) {
4717                 printf("%c", data.dptr[i]);
4718         }
4719         printf("\n");
4720 }
4721
4722 /*
4723   listen for messages on a messageport
4724  */
4725 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4726 {
4727         uint64_t srvid;
4728
4729         srvid = getpid();
4730
4731         /* register a message port and listen for messages
4732         */
4733         ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4734         printf("Listening for messages on srvid:%d\n", (int)srvid);
4735
4736         while (1) {     
4737                 event_loop_once(ctdb->ev);
4738         }
4739
4740         return 0;
4741 }
4742
4743 /*
4744   list all nodes in the cluster
4745   we parse the nodes file directly
4746  */
4747 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4748 {
4749         TALLOC_CTX *mem_ctx = talloc_new(NULL);
4750         struct pnn_node *pnn_nodes;
4751         struct pnn_node *pnn_node;
4752
4753         pnn_nodes = read_nodes_file(mem_ctx);
4754         if (pnn_nodes == NULL) {
4755                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4756                 talloc_free(mem_ctx);
4757                 return -1;
4758         }
4759
4760         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4761                 ctdb_sock_addr addr;
4762                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4763                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4764                         talloc_free(mem_ctx);
4765                         return -1;
4766                 }
4767                 if (options.machinereadable){
4768                         printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4769                 } else {
4770                         printf("%s\n", pnn_node->addr);
4771                 }
4772         }
4773         talloc_free(mem_ctx);
4774
4775         return 0;
4776 }
4777
4778 /*
4779   reload the nodes file on the local node
4780  */
4781 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4782 {
4783         int i, ret;
4784         int mypnn;
4785         struct ctdb_node_map *nodemap=NULL;
4786
4787         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4788         if (mypnn == -1) {
4789                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4790                 return -1;
4791         }
4792
4793         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4794         if (ret != 0) {
4795                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4796                 return ret;
4797         }
4798
4799         /* reload the nodes file on all remote nodes */
4800         for (i=0;i<nodemap->num;i++) {
4801                 if (nodemap->nodes[i].pnn == mypnn) {
4802                         continue;
4803                 }
4804                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4805                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4806                         nodemap->nodes[i].pnn);
4807                 if (ret != 0) {
4808                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4809                 }
4810         }
4811
4812         /* reload the nodes file on the local node */
4813         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4814         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4815         if (ret != 0) {
4816                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4817         }
4818
4819         /* initiate a recovery */
4820         control_recover(ctdb, argc, argv);
4821
4822         return 0;
4823 }
4824
4825
4826 static const struct {
4827         const char *name;
4828         int (*fn)(struct ctdb_context *, int, const char **);
4829         bool auto_all;
4830         bool without_daemon; /* can be run without daemon running ? */
4831         const char *msg;
4832         const char *args;
4833 } ctdb_commands[] = {
4834 #ifdef CTDB_VERS
4835         { "version",         control_version,           true,   false,  "show version of ctdb" },
4836 #endif
4837         { "status",          control_status,            true,   false,  "show node status" },
4838         { "uptime",          control_uptime,            true,   false,  "show node uptime" },
4839         { "ping",            control_ping,              true,   false,  "ping all nodes" },
4840         { "getvar",          control_getvar,            true,   false,  "get a tunable variable",               "<name>"},
4841         { "setvar",          control_setvar,            true,   false,  "set a tunable variable",               "<name> <value>"},
4842         { "listvars",        control_listvars,          true,   false,  "list tunable variables"},
4843         { "statistics",      control_statistics,        false,  false, "show statistics" },
4844         { "statisticsreset", control_statistics_reset,  true,   false,  "reset statistics"},
4845         { "stats",           control_stats,             false,  false,  "show rolling statistics", "[number of history records]" },
4846         { "ip",              control_ip,                false,  false,  "show which public ip's that ctdb manages" },
4847         { "ipinfo",          control_ipinfo,            true,   false,  "show details about a public ip that ctdb manages", "<ip>" },
4848         { "ifaces",          control_ifaces,            true,   false,  "show which interfaces that ctdb manages" },
4849         { "setifacelink",    control_setifacelink,      true,   false,  "set interface link status", "<iface> <status>" },
4850         { "process-exists",  control_process_exists,    true,   false,  "check if a process exists on a node",  "<pid>"},
4851         { "getdbmap",        control_getdbmap,          true,   false,  "show the database map" },
4852         { "getdbstatus",     control_getdbstatus,       true,   false,  "show the status of a database", "<dbname>" },
4853         { "catdb",           control_catdb,             true,   false,  "dump a database" ,                     "<dbname>"},
4854         { "getmonmode",      control_getmonmode,        true,   false,  "show monitoring mode" },
4855         { "getcapabilities", control_getcapabilities,   true,   false,  "show node capabilities" },
4856         { "pnn",             control_pnn,               true,   false,  "show the pnn of the currnet node" },
4857         { "lvs",             control_lvs,               true,   false,  "show lvs configuration" },
4858         { "lvsmaster",       control_lvsmaster,         true,   false,  "show which node is the lvs master" },
4859         { "disablemonitor",      control_disable_monmode,true,  false,  "set monitoring mode to DISABLE" },
4860         { "enablemonitor",      control_enable_monmode, true,   false,  "set monitoring mode to ACTIVE" },
4861         { "setdebug",        control_setdebug,          true,   false,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4862         { "getdebug",        control_getdebug,          true,   false,  "get debug level" },
4863         { "getlog",          control_getlog,            true,   false,  "get the log data from the in memory ringbuffer", "<level>" },
4864         { "clearlog",          control_clearlog,        true,   false,  "clear the log data from the in memory ringbuffer" },
4865         { "attach",          control_attach,            true,   false,  "attach to a database",                 "<dbname>" },
4866         { "dumpmemory",      control_dumpmemory,        true,   false,  "dump memory map to stdout" },
4867         { "rddumpmemory",    control_rddumpmemory,      true,   false,  "dump memory map from the recovery daemon to stdout" },
4868         { "getpid",          control_getpid,            true,   false,  "get ctdbd process ID" },
4869         { "disable",         control_disable,           true,   false,  "disable a nodes public IP" },
4870         { "enable",          control_enable,            true,   false,  "enable a nodes public IP" },
4871         { "stop",            control_stop,              true,   false,  "stop a node" },
4872         { "continue",        control_continue,          true,   false,  "re-start a stopped node" },
4873         { "ban",             control_ban,               true,   false,  "ban a node from the cluster",          "<bantime|0>"},
4874         { "unban",           control_unban,             true,   false,  "unban a node" },
4875         { "showban",         control_showban,           true,   false,  "show ban information"},
4876         { "shutdown",        control_shutdown,          true,   false,  "shutdown ctdbd" },
4877         { "recover",         control_recover,           true,   false,  "force recovery" },
4878         { "sync",            control_ipreallocate,      true,   false,  "wait until ctdbd has synced all state changes" },
4879         { "ipreallocate",    control_ipreallocate,      true,   false,  "force the recovery daemon to perform a ip reallocation procedure" },
4880         { "thaw",            control_thaw,              true,   false,  "thaw databases", "[priority:1-3]" },
4881         { "isnotrecmaster",  control_isnotrecmaster,    false,  false,  "check if the local node is recmaster or not" },
4882         { "killtcp",         kill_tcp,                  false,  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4883         { "gratiousarp",     control_gratious_arp,      false,  false, "send a gratious arp", "<ip> <interface>" },
4884         { "tickle",          tickle_tcp,                false,  false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4885         { "gettickles",      control_get_tickles,       false,  false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4886         { "addtickle",       control_add_tickle,        false,  false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4887
4888         { "deltickle",       control_del_tickle,        false,  false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4889
4890         { "regsrvid",        regsrvid,                  false,  false, "register a server id", "<pnn> <type> <id>" },
4891         { "unregsrvid",      unregsrvid,                false,  false, "unregister a server id", "<pnn> <type> <id>" },
4892         { "chksrvid",        chksrvid,                  false,  false, "check if a server id exists", "<pnn> <type> <id>" },
4893         { "getsrvids",       getsrvids,                 false,  false, "get a list of all server ids"},
4894         { "vacuum",          ctdb_vacuum,               false,  false, "vacuum the databases of empty records", "[max_records]"},
4895         { "repack",          ctdb_repack,               false,  false, "repack all databases", "[max_freelist]"},
4896         { "listnodes",       control_listnodes,         false,  true, "list all nodes in the cluster"},
4897         { "reloadnodes",     control_reload_nodes_file, false,  false, "reload the nodes file and restart the transport on all nodes"},
4898         { "moveip",          control_moveip,            false,  false, "move/failover an ip address to another node", "<ip> <node>"},
4899         { "addip",           control_addip,             true,   false, "add a ip address to a node", "<ip/mask> <iface>"},
4900         { "delip",           control_delip,             false,  false, "delete an ip address from a node", "<ip>"},
4901         { "eventscript",     control_eventscript,       true,   false, "run the eventscript with the given parameters on a node", "<arguments>"},
4902         { "backupdb",        control_backupdb,          false,  false, "backup the database into a file.", "<database> <file>"},
4903         { "restoredb",        control_restoredb,        false,  false, "restore the database from a file.", "<file> [dbname]"},
4904         { "dumpdbbackup",    control_dumpdbbackup,      false,  true,  "dump database backup from a file.", "<file>"},
4905         { "wipedb",           control_wipedb,        false,     false, "wipe the contents of a database.", "<dbname>"},
4906         { "recmaster",        control_recmaster,        false,  false, "show the pnn for the recovery master."},
4907         { "scriptstatus",    control_scriptstatus,  false,      false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
4908         { "enablescript",     control_enablescript,  false,     false, "enable an eventscript", "<script>"},
4909         { "disablescript",    control_disablescript,  false,    false, "disable an eventscript", "<script>"},
4910         { "natgwlist",        control_natgwlist,        false,  false, "show the nodes belonging to this natgw configuration"},
4911         { "xpnn",             control_xpnn,             true,   true,  "find the pnn of the local node without talking to the daemon (unreliable)" },
4912         { "getreclock",       control_getreclock,       false,  false, "Show the reclock file of a node"},
4913         { "setreclock",       control_setreclock,       false,  false, "Set/clear the reclock file of a node", "[filename]"},
4914         { "setnatgwstate",    control_setnatgwstate,    false,  false, "Set NATGW state to on/off", "{on|off}"},
4915         { "setlmasterrole",   control_setlmasterrole,   false,  false, "Set LMASTER role to on/off", "{on|off}"},
4916         { "setrecmasterrole", control_setrecmasterrole, false,  false, "Set RECMASTER role to on/off", "{on|off}"},
4917         { "setdbprio",        control_setdbprio,        false,  false, "Set DB priority", "<dbid> <prio:1-3>"},
4918         { "getdbprio",        control_getdbprio,        false,  false, "Get DB priority", "<dbid>"},
4919         { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
4920         { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
4921         { "sync",            control_ipreallocate,      false,  false,  "wait until ctdbd has synced all state changes" },
4922         { "pfetch",          control_pfetch,            false,  false,  "fetch a record from a persistent database", "<db> <key> [<file>]" },
4923         { "pstore",          control_pstore,            false,  false,  "write a record to a persistent database", "<db> <key> <file containing record>" },
4924         { "tfetch",          control_tfetch,            false,  true,  "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
4925         { "readkey",         control_readkey,           true,   false,  "read the content off a database key", "<tdb-file> <key>" },
4926         { "writekey",        control_writekey,          true,   false,  "write to a database key", "<tdb-file> <key> <value>" },
4927 };
4928
4929 /*
4930   show usage message
4931  */
4932 static void usage(void)
4933 {
4934         int i;
4935         printf(
4936 "Usage: ctdb [options] <control>\n" \
4937 "Options:\n" \
4938 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
4939 "   -Y                 generate machinereadable output\n"
4940 "   -v                 generate verbose output\n"
4941 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
4942         printf("Controls:\n");
4943         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4944                 printf("  %-15s %-27s  %s\n", 
4945                        ctdb_commands[i].name, 
4946                        ctdb_commands[i].args?ctdb_commands[i].args:"",
4947                        ctdb_commands[i].msg);
4948         }
4949         exit(1);
4950 }
4951
4952
4953 static void ctdb_alarm(int sig)
4954 {
4955         printf("Maximum runtime exceeded - exiting\n");
4956         _exit(ERR_TIMEOUT);
4957 }
4958
4959 /*
4960   main program
4961 */
4962 int main(int argc, const char *argv[])
4963 {
4964         struct ctdb_context *ctdb;
4965         char *nodestring = NULL;
4966         struct poptOption popt_options[] = {
4967                 POPT_AUTOHELP
4968                 POPT_CTDB_CMDLINE
4969                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
4970                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
4971                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
4972                 { "verbose",    'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
4973                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
4974                 POPT_TABLEEND
4975         };
4976         int opt;
4977         const char **extra_argv;
4978         int extra_argc = 0;
4979         int ret=-1, i;
4980         poptContext pc;
4981         struct event_context *ev;
4982         const char *control;
4983
4984         setlinebuf(stdout);
4985         
4986         /* set some defaults */
4987         options.maxruntime = 0;
4988         options.timelimit = 3;
4989         options.pnn = CTDB_CURRENT_NODE;
4990
4991         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
4992
4993         while ((opt = poptGetNextOpt(pc)) != -1) {
4994                 switch (opt) {
4995                 default:
4996                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
4997                                 poptBadOption(pc, 0), poptStrerror(opt)));
4998                         exit(1);
4999                 }
5000         }
5001
5002         /* setup the remaining options for the main program to use */
5003         extra_argv = poptGetArgs(pc);
5004         if (extra_argv) {
5005                 extra_argv++;
5006                 while (extra_argv[extra_argc]) extra_argc++;
5007         }
5008
5009         if (extra_argc < 1) {
5010                 usage();
5011         }
5012
5013         if (options.maxruntime == 0) {
5014                 const char *ctdb_timeout;
5015                 ctdb_timeout = getenv("CTDB_TIMEOUT");
5016                 if (ctdb_timeout != NULL) {
5017                         options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5018                 } else {
5019                         /* default timeout is 120 seconds */
5020                         options.maxruntime = 120;
5021                 }
5022         }
5023
5024         signal(SIGALRM, ctdb_alarm);
5025         alarm(options.maxruntime);
5026
5027         /* setup the node number to contact */
5028         if (nodestring != NULL) {
5029                 if (strcmp(nodestring, "all") == 0) {
5030                         options.pnn = CTDB_BROADCAST_ALL;
5031                 } else {
5032                         options.pnn = strtoul(nodestring, NULL, 0);
5033                 }
5034         }
5035
5036         control = extra_argv[0];
5037
5038         ev = event_context_init(NULL);
5039         if (!ev) {
5040                 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5041                 exit(1);
5042         }
5043         tevent_loop_allow_nesting(ev);
5044
5045         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5046                 if (strcmp(control, ctdb_commands[i].name) == 0) {
5047                         int j;
5048
5049                         if (ctdb_commands[i].without_daemon == true) {
5050                                 close(2);
5051                         }
5052
5053                         if (ctdb_commands[i].without_daemon == false) {
5054                                 const char *socket_name;
5055
5056                                 /* initialise ctdb */
5057                                 ctdb = ctdb_cmdline_client(ev);
5058
5059                                 if (ctdb == NULL) {
5060                                         DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5061                                         exit(1);
5062                                 }
5063
5064                                 /* initialize a libctdb connection as well */
5065                                 socket_name = ctdb_get_socketname(ctdb);
5066                                 ctdb_connection = ctdb_connect(socket_name,
5067                                                        ctdb_log_file, stderr);
5068                                 if (ctdb_connection == NULL) {
5069                                         fprintf(stderr, "Failed to connect to daemon from libctdb\n");
5070                                         exit(1);
5071                                 }                               
5072                         
5073                                 /* verify the node exists */
5074                                 verify_node(ctdb);
5075
5076                                 if (options.pnn == CTDB_CURRENT_NODE) {
5077                                         int pnn;
5078                                         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
5079                                         if (pnn == -1) {
5080                                                 return -1;
5081                                         }
5082                                         options.pnn = pnn;
5083                                 }
5084                         }
5085
5086                         if (ctdb_commands[i].auto_all && 
5087                             options.pnn == CTDB_BROADCAST_ALL) {
5088                                 uint32_t *nodes;
5089                                 uint32_t num_nodes;
5090                                 ret = 0;
5091
5092                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5093                                 CTDB_NO_MEMORY(ctdb, nodes);
5094         
5095                                 for (j=0;j<num_nodes;j++) {
5096                                         options.pnn = nodes[j];
5097                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5098                                 }
5099                                 talloc_free(nodes);
5100                         } else {
5101                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5102                         }
5103                         break;
5104                 }
5105         }
5106
5107         if (i == ARRAY_SIZE(ctdb_commands)) {
5108                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5109                 exit(1);
5110         }
5111
5112         return ret;
5113 }