We default to non-deterministic ip now where ips are "sticky" and dont change
[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         unsigned mask;
1630         ctdb_sock_addr addr;
1631         struct ctdb_control_ip_iface *pub;
1632         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1633         struct ctdb_all_public_ips *ips;
1634
1635
1636         if (argc != 2) {
1637                 talloc_free(tmp_ctx);
1638                 usage();
1639         }
1640
1641         if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
1642                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
1643                 talloc_free(tmp_ctx);
1644                 return -1;
1645         }
1646
1647         /* read the public ip list from the node */
1648         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1649         if (ret != 0) {
1650                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
1651                 talloc_free(tmp_ctx);
1652                 return -1;
1653         }
1654         for (i=0;i<ips->num;i++) {
1655                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1656                         DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
1657                         return 0;
1658                 }
1659         }
1660
1661
1662
1663         /* Dont timeout. This command waits for an ip reallocation
1664            which sometimes can take wuite a while if there has
1665            been a recent recovery
1666         */
1667         alarm(0);
1668
1669         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
1670         pub = talloc_size(tmp_ctx, len); 
1671         CTDB_NO_MEMORY(ctdb, pub);
1672
1673         pub->addr  = addr;
1674         pub->mask  = mask;
1675         pub->len   = strlen(argv[1])+1;
1676         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
1677
1678         do {
1679                 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
1680                 if (ret != 0) {
1681                         DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
1682                         sleep(3);
1683                         retries++;
1684                 }
1685         } while (retries < 5 && ret != 0);
1686         if (ret != 0) {
1687                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
1688                 talloc_free(tmp_ctx);
1689                 return ret;
1690         }
1691
1692         do {
1693                 ret = control_ipreallocate(ctdb, argc, argv);
1694                 if (ret != 0) {
1695                         DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Wait 3 seconds and try again.\n", options.pnn));
1696                         sleep(3);
1697                         retries++;
1698                 }
1699         } while (retries < 5 && ret != 0);
1700         if (ret != 0) {
1701                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u. Giving up.\n", options.pnn));
1702                 talloc_free(tmp_ctx);
1703                 return ret;
1704         }
1705
1706         talloc_free(tmp_ctx);
1707         return 0;
1708 }
1709
1710 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
1711
1712 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
1713 {
1714         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1715         struct ctdb_node_map *nodemap=NULL;
1716         struct ctdb_all_public_ips *ips;
1717         int ret, i, j;
1718
1719         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1720         if (ret != 0) {
1721                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
1722                 return ret;
1723         }
1724
1725         /* remove it from the nodes that are not hosting the ip currently */
1726         for(i=0;i<nodemap->num;i++){
1727                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1728                         continue;
1729                 }
1730                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1731                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1732                         continue;
1733                 }
1734
1735                 for (j=0;j<ips->num;j++) {
1736                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1737                                 break;
1738                         }
1739                 }
1740                 if (j==ips->num) {
1741                         continue;
1742                 }
1743
1744                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
1745                         continue;
1746                 }
1747
1748                 options.pnn = nodemap->nodes[i].pnn;
1749                 control_delip(ctdb, argc, argv);
1750         }
1751
1752
1753         /* remove it from every node (also the one hosting it) */
1754         for(i=0;i<nodemap->num;i++){
1755                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1756                         continue;
1757                 }
1758                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
1759                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
1760                         continue;
1761                 }
1762
1763                 for (j=0;j<ips->num;j++) {
1764                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1765                                 break;
1766                         }
1767                 }
1768                 if (j==ips->num) {
1769                         continue;
1770                 }
1771
1772                 options.pnn = nodemap->nodes[i].pnn;
1773                 control_delip(ctdb, argc, argv);
1774         }
1775
1776         talloc_free(tmp_ctx);
1777         return 0;
1778 }
1779         
1780 /*
1781   delete a public ip address from a node
1782  */
1783 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
1784 {
1785         int i, ret;
1786         int retries = 0;
1787         ctdb_sock_addr addr;
1788         struct ctdb_control_ip_iface pub;
1789         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1790         struct ctdb_all_public_ips *ips;
1791
1792         if (argc != 1) {
1793                 talloc_free(tmp_ctx);
1794                 usage();
1795         }
1796
1797         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1798                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1799                 return -1;
1800         }
1801
1802         if (options.pnn == CTDB_BROADCAST_ALL) {
1803                 return control_delip_all(ctdb, argc, argv, &addr);
1804         }
1805
1806         pub.addr  = addr;
1807         pub.mask  = 0;
1808         pub.len   = 0;
1809
1810         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1811         if (ret != 0) {
1812                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
1813                 talloc_free(tmp_ctx);
1814                 return ret;
1815         }
1816         
1817         for (i=0;i<ips->num;i++) {
1818                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
1819                         break;
1820                 }
1821         }
1822
1823         if (i==ips->num) {
1824                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
1825                         ctdb_addr_to_str(&addr)));
1826                 talloc_free(tmp_ctx);
1827                 return -1;
1828         }
1829
1830         if (ips->ips[i].pnn == options.pnn) {
1831                 ret = find_other_host_for_public_ip(ctdb, &addr);
1832                 if (ret != -1) {
1833                         do {
1834                                 ret = move_ip(ctdb, &addr, ret);
1835                                 if (ret != 0) {
1836                                         DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Wait 3 seconds and try again.\n", options.pnn));
1837                                         sleep(3);
1838                                         retries++;
1839                                 }
1840                         } while (retries < 5 && ret != 0);
1841                         if (ret != 0) {
1842                                 DEBUG(DEBUG_ERR,("Failed to move ip to node %d. Giving up.\n", options.pnn));
1843                                 return -1;
1844                         }
1845                 }
1846         }
1847
1848         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
1849         if (ret != 0) {
1850                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
1851                 talloc_free(tmp_ctx);
1852                 return ret;
1853         }
1854
1855         talloc_free(tmp_ctx);
1856         return 0;
1857 }
1858
1859 /*
1860   kill a tcp connection
1861  */
1862 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1863 {
1864         int ret;
1865         struct ctdb_control_killtcp killtcp;
1866
1867         if (argc < 2) {
1868                 usage();
1869         }
1870
1871         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1872                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1873                 return -1;
1874         }
1875
1876         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1877                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1878                 return -1;
1879         }
1880
1881         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1882         if (ret != 0) {
1883                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1884                 return ret;
1885         }
1886
1887         return 0;
1888 }
1889
1890
1891 /*
1892   send a gratious arp
1893  */
1894 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1895 {
1896         int ret;
1897         ctdb_sock_addr addr;
1898
1899         if (argc < 2) {
1900                 usage();
1901         }
1902
1903         if (!parse_ip(argv[0], NULL, 0, &addr)) {
1904                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1905                 return -1;
1906         }
1907
1908         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1909         if (ret != 0) {
1910                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1911                 return ret;
1912         }
1913
1914         return 0;
1915 }
1916
1917 /*
1918   register a server id
1919  */
1920 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1921 {
1922         int ret;
1923         struct ctdb_server_id server_id;
1924
1925         if (argc < 3) {
1926                 usage();
1927         }
1928
1929         server_id.pnn       = strtoul(argv[0], NULL, 0);
1930         server_id.type      = strtoul(argv[1], NULL, 0);
1931         server_id.server_id = strtoul(argv[2], NULL, 0);
1932
1933         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1934         if (ret != 0) {
1935                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1936                 return ret;
1937         }
1938         DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
1939         sleep(999);
1940         return -1;
1941 }
1942
1943 /*
1944   unregister a server id
1945  */
1946 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1947 {
1948         int ret;
1949         struct ctdb_server_id server_id;
1950
1951         if (argc < 3) {
1952                 usage();
1953         }
1954
1955         server_id.pnn       = strtoul(argv[0], NULL, 0);
1956         server_id.type      = strtoul(argv[1], NULL, 0);
1957         server_id.server_id = strtoul(argv[2], NULL, 0);
1958
1959         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1960         if (ret != 0) {
1961                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1962                 return ret;
1963         }
1964         return -1;
1965 }
1966
1967 /*
1968   check if a server id exists
1969  */
1970 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1971 {
1972         uint32_t status;
1973         int ret;
1974         struct ctdb_server_id server_id;
1975
1976         if (argc < 3) {
1977                 usage();
1978         }
1979
1980         server_id.pnn       = strtoul(argv[0], NULL, 0);
1981         server_id.type      = strtoul(argv[1], NULL, 0);
1982         server_id.server_id = strtoul(argv[2], NULL, 0);
1983
1984         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1985         if (ret != 0) {
1986                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1987                 return ret;
1988         }
1989
1990         if (status) {
1991                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1992         } else {
1993                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1994         }
1995         return 0;
1996 }
1997
1998 /*
1999   get a list of all server ids that are registered on a node
2000  */
2001 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2002 {
2003         int i, ret;
2004         struct ctdb_server_id_list *server_ids;
2005
2006         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2007         if (ret != 0) {
2008                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2009                 return ret;
2010         }
2011
2012         for (i=0; i<server_ids->num; i++) {
2013                 printf("Server id %d:%d:%d\n", 
2014                         server_ids->server_ids[i].pnn, 
2015                         server_ids->server_ids[i].type, 
2016                         server_ids->server_ids[i].server_id); 
2017         }
2018
2019         return -1;
2020 }
2021
2022 /*
2023   send a tcp tickle ack
2024  */
2025 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2026 {
2027         int ret;
2028         ctdb_sock_addr  src, dst;
2029
2030         if (argc < 2) {
2031                 usage();
2032         }
2033
2034         if (!parse_ip_port(argv[0], &src)) {
2035                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2036                 return -1;
2037         }
2038
2039         if (!parse_ip_port(argv[1], &dst)) {
2040                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2041                 return -1;
2042         }
2043
2044         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2045         if (ret==0) {
2046                 return 0;
2047         }
2048         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2049
2050         return -1;
2051 }
2052
2053
2054 /*
2055   display public ip status
2056  */
2057 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2058 {
2059         int i, ret;
2060         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2061         struct ctdb_all_public_ips *ips;
2062
2063         if (options.pnn == CTDB_BROADCAST_ALL) {
2064                 /* read the list of public ips from all nodes */
2065                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2066         } else {
2067                 /* read the public ip list from this node */
2068                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2069         }
2070         if (ret != 0) {
2071                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2072                 talloc_free(tmp_ctx);
2073                 return ret;
2074         }
2075
2076         if (options.machinereadable){
2077                 printf(":Public IP:Node:");
2078                 if (options.verbose){
2079                         printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2080                 }
2081                 printf("\n");
2082         } else {
2083                 if (options.pnn == CTDB_BROADCAST_ALL) {
2084                         printf("Public IPs on ALL nodes\n");
2085                 } else {
2086                         printf("Public IPs on node %u\n", options.pnn);
2087                 }
2088         }
2089
2090         for (i=1;i<=ips->num;i++) {
2091                 struct ctdb_control_public_ip_info *info = NULL;
2092                 int32_t pnn;
2093                 char *aciface = NULL;
2094                 char *avifaces = NULL;
2095                 char *cifaces = NULL;
2096
2097                 if (options.pnn == CTDB_BROADCAST_ALL) {
2098                         pnn = ips->ips[ips->num-i].pnn;
2099                 } else {
2100                         pnn = options.pnn;
2101                 }
2102
2103                 if (pnn != -1) {
2104                         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2105                                                    &ips->ips[ips->num-i].addr, &info);
2106                 } else {
2107                         ret = -1;
2108                 }
2109
2110                 if (ret == 0) {
2111                         int j;
2112                         for (j=0; j < info->num; j++) {
2113                                 if (cifaces == NULL) {
2114                                         cifaces = talloc_strdup(info,
2115                                                                 info->ifaces[j].name);
2116                                 } else {
2117                                         cifaces = talloc_asprintf_append(cifaces,
2118                                                                          ",%s",
2119                                                                          info->ifaces[j].name);
2120                                 }
2121
2122                                 if (info->active_idx == j) {
2123                                         aciface = info->ifaces[j].name;
2124                                 }
2125
2126                                 if (info->ifaces[j].link_state == 0) {
2127                                         continue;
2128                                 }
2129
2130                                 if (avifaces == NULL) {
2131                                         avifaces = talloc_strdup(info, info->ifaces[j].name);
2132                                 } else {
2133                                         avifaces = talloc_asprintf_append(avifaces,
2134                                                                           ",%s",
2135                                                                           info->ifaces[j].name);
2136                                 }
2137                         }
2138                 }
2139
2140                 if (options.machinereadable){
2141                         printf(":%s:%d:",
2142                                 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2143                                 ips->ips[ips->num-i].pnn);
2144                         if (options.verbose){
2145                                 printf("%s:%s:%s:",
2146                                         aciface?aciface:"",
2147                                         avifaces?avifaces:"",
2148                                         cifaces?cifaces:"");
2149                         }
2150                         printf("\n");
2151                 } else {
2152                         if (options.verbose) {
2153                                 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2154                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2155                                         ips->ips[ips->num-i].pnn,
2156                                         aciface?aciface:"",
2157                                         avifaces?avifaces:"",
2158                                         cifaces?cifaces:"");
2159                         } else {
2160                                 printf("%s %d\n",
2161                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2162                                         ips->ips[ips->num-i].pnn);
2163                         }
2164                 }
2165                 talloc_free(info);
2166         }
2167
2168         talloc_free(tmp_ctx);
2169         return 0;
2170 }
2171
2172 /*
2173   public ip info
2174  */
2175 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2176 {
2177         int i, ret;
2178         ctdb_sock_addr addr;
2179         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2180         struct ctdb_control_public_ip_info *info;
2181
2182         if (argc != 1) {
2183                 talloc_free(tmp_ctx);
2184                 usage();
2185         }
2186
2187         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2188                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2189                 return -1;
2190         }
2191
2192         /* read the public ip info from this node */
2193         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2194                                            tmp_ctx, &addr, &info);
2195         if (ret != 0) {
2196                 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2197                                   argv[0], options.pnn));
2198                 talloc_free(tmp_ctx);
2199                 return ret;
2200         }
2201
2202         printf("Public IP[%s] info on node %u\n",
2203                ctdb_addr_to_str(&info->ip.addr),
2204                options.pnn);
2205
2206         printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2207                ctdb_addr_to_str(&info->ip.addr),
2208                info->ip.pnn, info->num);
2209
2210         for (i=0; i<info->num; i++) {
2211                 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2212
2213                 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2214                        i+1, info->ifaces[i].name,
2215                        info->ifaces[i].link_state?"up":"down",
2216                        (unsigned int)info->ifaces[i].references,
2217                        (i==info->active_idx)?" (active)":"");
2218         }
2219
2220         talloc_free(tmp_ctx);
2221         return 0;
2222 }
2223
2224 /*
2225   display interfaces status
2226  */
2227 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2228 {
2229         int i, ret;
2230         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2231         struct ctdb_control_get_ifaces *ifaces;
2232
2233         /* read the public ip list from this node */
2234         ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn,
2235                                    tmp_ctx, &ifaces);
2236         if (ret != 0) {
2237                 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2238                                   options.pnn));
2239                 talloc_free(tmp_ctx);
2240                 return ret;
2241         }
2242
2243         if (options.machinereadable){
2244                 printf(":Name:LinkStatus:References:\n");
2245         } else {
2246                 printf("Interfaces on node %u\n", options.pnn);
2247         }
2248
2249         for (i=0; i<ifaces->num; i++) {
2250                 if (options.machinereadable){
2251                         printf(":%s:%s:%u\n",
2252                                ifaces->ifaces[i].name,
2253                                ifaces->ifaces[i].link_state?"1":"0",
2254                                (unsigned int)ifaces->ifaces[i].references);
2255                 } else {
2256                         printf("name:%s link:%s references:%u\n",
2257                                ifaces->ifaces[i].name,
2258                                ifaces->ifaces[i].link_state?"up":"down",
2259                                (unsigned int)ifaces->ifaces[i].references);
2260                 }
2261         }
2262
2263         talloc_free(tmp_ctx);
2264         return 0;
2265 }
2266
2267
2268 /*
2269   set link status of an interface
2270  */
2271 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
2272 {
2273         int ret;
2274         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2275         struct ctdb_control_iface_info info;
2276
2277         ZERO_STRUCT(info);
2278
2279         if (argc != 2) {
2280                 usage();
2281         }
2282
2283         if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
2284                 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
2285                                   argv[0]));
2286                 talloc_free(tmp_ctx);
2287                 return -1;
2288         }
2289         strcpy(info.name, argv[0]);
2290
2291         if (strcmp(argv[1], "up") == 0) {
2292                 info.link_state = 1;
2293         } else if (strcmp(argv[1], "down") == 0) {
2294                 info.link_state = 0;
2295         } else {
2296                 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
2297                                   argv[1]));
2298                 talloc_free(tmp_ctx);
2299                 return -1;
2300         }
2301
2302         /* read the public ip list from this node */
2303         ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
2304                                    tmp_ctx, &info);
2305         if (ret != 0) {
2306                 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
2307                                   argv[0], options.pnn));
2308                 talloc_free(tmp_ctx);
2309                 return ret;
2310         }
2311
2312         talloc_free(tmp_ctx);
2313         return 0;
2314 }
2315
2316 /*
2317   display pid of a ctdb daemon
2318  */
2319 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
2320 {
2321         uint32_t pid;
2322         int ret;
2323
2324         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
2325         if (ret != 0) {
2326                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
2327                 return ret;
2328         }
2329         printf("Pid:%d\n", pid);
2330
2331         return 0;
2332 }
2333
2334 /*
2335   disable a remote node
2336  */
2337 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
2338 {
2339         int ret;
2340         struct ctdb_node_map *nodemap=NULL;
2341
2342         /* check if the node is already disabled */
2343         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2344                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2345                 exit(10);
2346         }
2347         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2348                 DEBUG(DEBUG_ERR,("Node %d is already disabled.\n", options.pnn));
2349                 return 0;
2350         }
2351
2352         do {
2353                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2354                 if (ret != 0) {
2355                         DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
2356                         return ret;
2357                 }
2358
2359                 sleep(1);
2360
2361                 /* read the nodemap and verify the change took effect */
2362                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2363                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2364                         exit(10);
2365                 }
2366
2367         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED));
2368         ret = control_ipreallocate(ctdb, argc, argv);
2369         if (ret != 0) {
2370                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2371                 return ret;
2372         }
2373
2374         return 0;
2375 }
2376
2377 /*
2378   enable a disabled remote node
2379  */
2380 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
2381 {
2382         int ret;
2383
2384         struct ctdb_node_map *nodemap=NULL;
2385
2386
2387         /* check if the node is already enabled */
2388         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2389                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2390                 exit(10);
2391         }
2392         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED)) {
2393                 DEBUG(DEBUG_ERR,("Node %d is already enabled.\n", options.pnn));
2394                 return 0;
2395         }
2396
2397         do {
2398                 ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2399                 if (ret != 0) {
2400                         DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
2401                         return ret;
2402                 }
2403
2404                 sleep(1);
2405
2406                 /* read the nodemap and verify the change took effect */
2407                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2408                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2409                         exit(10);
2410                 }
2411
2412         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_PERMANENTLY_DISABLED);
2413
2414         ret = control_ipreallocate(ctdb, argc, argv);
2415         if (ret != 0) {
2416                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2417                 return ret;
2418         }
2419
2420         return 0;
2421 }
2422
2423 /*
2424   stop a remote node
2425  */
2426 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
2427 {
2428         int ret;
2429         struct ctdb_node_map *nodemap=NULL;
2430
2431         do {
2432                 ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
2433                 if (ret != 0) {
2434                         DEBUG(DEBUG_ERR, ("Unable to stop node %u   try again\n", options.pnn));
2435                 }
2436         
2437                 sleep(1);
2438
2439                 /* read the nodemap and verify the change took effect */
2440                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2441                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2442                         exit(10);
2443                 }
2444
2445         } while (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED));
2446         ret = control_ipreallocate(ctdb, argc, argv);
2447         if (ret != 0) {
2448                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2449                 return ret;
2450         }
2451
2452         return 0;
2453 }
2454
2455 /*
2456   restart a stopped remote node
2457  */
2458 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
2459 {
2460         int ret;
2461
2462         struct ctdb_node_map *nodemap=NULL;
2463
2464         do {
2465                 ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
2466                 if (ret != 0) {
2467                         DEBUG(DEBUG_ERR, ("Unable to continue node %u\n", options.pnn));
2468                         return ret;
2469                 }
2470         
2471                 sleep(1);
2472
2473                 /* read the nodemap and verify the change took effect */
2474                 if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
2475                         DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2476                         exit(10);
2477                 }
2478
2479         } while (nodemap->nodes[options.pnn].flags & NODE_FLAGS_STOPPED);
2480         ret = control_ipreallocate(ctdb, argc, argv);
2481         if (ret != 0) {
2482                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2483                 return ret;
2484         }
2485
2486         return 0;
2487 }
2488
2489 static uint32_t get_generation(struct ctdb_context *ctdb)
2490 {
2491         struct ctdb_vnn_map *vnnmap=NULL;
2492         int ret;
2493
2494         /* wait until the recmaster is not in recovery mode */
2495         while (1) {
2496                 uint32_t recmode, recmaster;
2497                 
2498                 if (vnnmap != NULL) {
2499                         talloc_free(vnnmap);
2500                         vnnmap = NULL;
2501                 }
2502
2503                 /* get the recmaster */
2504                 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
2505                 if (ret != 0) {
2506                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
2507                         exit(10);
2508                 }
2509
2510                 /* get recovery mode */
2511                 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
2512                 if (ret != 0) {
2513                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
2514                         exit(10);
2515                 }
2516
2517                 /* get the current generation number */
2518                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
2519                 if (ret != 0) {
2520                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
2521                         exit(10);
2522                 }
2523
2524                 if ((recmode == CTDB_RECOVERY_NORMAL)
2525                 &&  (vnnmap->generation != 1)){
2526                         return vnnmap->generation;
2527                 }
2528                 sleep(1);
2529         }
2530 }
2531
2532 /*
2533   ban a node from the cluster
2534  */
2535 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
2536 {
2537         int ret;
2538         struct ctdb_node_map *nodemap=NULL;
2539         struct ctdb_ban_time bantime;
2540
2541         if (argc < 1) {
2542                 usage();
2543         }
2544         
2545         /* verify the node exists */
2546         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2547         if (ret != 0) {
2548                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2549                 return ret;
2550         }
2551
2552         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
2553                 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
2554                 return -1;
2555         }
2556
2557         bantime.pnn  = options.pnn;
2558         bantime.time = strtoul(argv[0], NULL, 0);
2559
2560         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2561         if (ret != 0) {
2562                 DEBUG(DEBUG_ERR,("Banning node %d for %d seconds failed.\n", bantime.pnn, bantime.time));
2563                 return -1;
2564         }       
2565
2566         ret = control_ipreallocate(ctdb, argc, argv);
2567         if (ret != 0) {
2568                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2569                 return ret;
2570         }
2571
2572         return 0;
2573 }
2574
2575
2576 /*
2577   unban a node from the cluster
2578  */
2579 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
2580 {
2581         int ret;
2582         struct ctdb_node_map *nodemap=NULL;
2583         struct ctdb_ban_time bantime;
2584
2585         /* verify the node exists */
2586         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2587         if (ret != 0) {
2588                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2589                 return ret;
2590         }
2591
2592         if (!(nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED)) {
2593                 DEBUG(DEBUG_ERR,("Node %u is not banned.\n", options.pnn));
2594                 return -1;
2595         }
2596
2597         bantime.pnn  = options.pnn;
2598         bantime.time = 0;
2599
2600         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, &bantime);
2601         if (ret != 0) {
2602                 DEBUG(DEBUG_ERR,("Unbanning node %d failed.\n", bantime.pnn));
2603                 return -1;
2604         }       
2605
2606         ret = control_ipreallocate(ctdb, argc, argv);
2607         if (ret != 0) {
2608                 DEBUG(DEBUG_ERR, ("IP Reallocate failed on node %u\n", options.pnn));
2609                 return ret;
2610         }
2611
2612         return 0;
2613 }
2614
2615
2616 /*
2617   show ban information for a node
2618  */
2619 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
2620 {
2621         int ret;
2622         struct ctdb_node_map *nodemap=NULL;
2623         struct ctdb_ban_time *bantime;
2624
2625         /* verify the node exists */
2626         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2627         if (ret != 0) {
2628                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2629                 return ret;
2630         }
2631
2632         ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
2633         if (ret != 0) {
2634                 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
2635                 return -1;
2636         }       
2637
2638         if (bantime->time == 0) {
2639                 printf("Node %u is not banned\n", bantime->pnn);
2640         } else {
2641                 printf("Node %u is banned banned for %d seconds\n", bantime->pnn, bantime->time);
2642         }
2643
2644         return 0;
2645 }
2646
2647 /*
2648   shutdown a daemon
2649  */
2650 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
2651 {
2652         int ret;
2653
2654         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
2655         if (ret != 0) {
2656                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
2657                 return ret;
2658         }
2659
2660         return 0;
2661 }
2662
2663 /*
2664   trigger a recovery
2665  */
2666 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
2667 {
2668         int ret;
2669         uint32_t generation, next_generation;
2670
2671         /* record the current generation number */
2672         generation = get_generation(ctdb);
2673
2674         ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
2675         if (ret != 0) {
2676                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
2677                 return ret;
2678         }
2679
2680         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2681         if (ret != 0) {
2682                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
2683                 return ret;
2684         }
2685
2686         /* wait until we are in a new generation */
2687         while (1) {
2688                 next_generation = get_generation(ctdb);
2689                 if (next_generation != generation) {
2690                         return 0;
2691                 }
2692                 sleep(1);
2693         }
2694
2695         return 0;
2696 }
2697
2698
2699 /*
2700   display monitoring mode of a remote node
2701  */
2702 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
2703 {
2704         uint32_t monmode;
2705         int ret;
2706
2707         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
2708         if (ret != 0) {
2709                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
2710                 return ret;
2711         }
2712         if (!options.machinereadable){
2713                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
2714         } else {
2715                 printf(":mode:\n");
2716                 printf(":%d:\n",monmode);
2717         }
2718         return 0;
2719 }
2720
2721
2722 /*
2723   display capabilities of a remote node
2724  */
2725 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
2726 {
2727         uint32_t capabilities;
2728         int ret;
2729
2730         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
2731         if (ret != 0) {
2732                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
2733                 return ret;
2734         }
2735         
2736         if (!options.machinereadable){
2737                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
2738                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
2739                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
2740                 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
2741         } else {
2742                 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
2743                 printf(":%d:%d:%d:%d:\n",
2744                         !!(capabilities&CTDB_CAP_RECMASTER),
2745                         !!(capabilities&CTDB_CAP_LMASTER),
2746                         !!(capabilities&CTDB_CAP_LVS),
2747                         !!(capabilities&CTDB_CAP_NATGW));
2748         }
2749         return 0;
2750 }
2751
2752 /*
2753   display lvs configuration
2754  */
2755 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
2756 {
2757         uint32_t *capabilities;
2758         struct ctdb_node_map *nodemap=NULL;
2759         int i, ret;
2760         int healthy_count = 0;
2761
2762         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2763         if (ret != 0) {
2764                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2765                 return ret;
2766         }
2767
2768         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2769         CTDB_NO_MEMORY(ctdb, capabilities);
2770         
2771         /* collect capabilities for all connected nodes */
2772         for (i=0; i<nodemap->num; i++) {
2773                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2774                         continue;
2775                 }
2776                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2777                         continue;
2778                 }
2779         
2780                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2781                 if (ret != 0) {
2782                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2783                         return ret;
2784                 }
2785
2786                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2787                         continue;
2788                 }
2789
2790                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2791                         healthy_count++;
2792                 }
2793         }
2794
2795         /* Print all LVS nodes */
2796         for (i=0; i<nodemap->num; i++) {
2797                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2798                         continue;
2799                 }
2800                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2801                         continue;
2802                 }
2803                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2804                         continue;
2805                 }
2806
2807                 if (healthy_count != 0) {
2808                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2809                                 continue;
2810                         }
2811                 }
2812
2813                 printf("%d:%s\n", i, 
2814                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
2815         }
2816
2817         return 0;
2818 }
2819
2820 /*
2821   display who is the lvs master
2822  */
2823 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
2824 {
2825         uint32_t *capabilities;
2826         struct ctdb_node_map *nodemap=NULL;
2827         int i, ret;
2828         int healthy_count = 0;
2829
2830         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2831         if (ret != 0) {
2832                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2833                 return ret;
2834         }
2835
2836         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
2837         CTDB_NO_MEMORY(ctdb, capabilities);
2838         
2839         /* collect capabilities for all connected nodes */
2840         for (i=0; i<nodemap->num; i++) {
2841                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2842                         continue;
2843                 }
2844                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2845                         continue;
2846                 }
2847         
2848                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
2849                 if (ret != 0) {
2850                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
2851                         return ret;
2852                 }
2853
2854                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2855                         continue;
2856                 }
2857
2858                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
2859                         healthy_count++;
2860                 }
2861         }
2862
2863         /* find and show the lvsmaster */
2864         for (i=0; i<nodemap->num; i++) {
2865                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2866                         continue;
2867                 }
2868                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
2869                         continue;
2870                 }
2871                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
2872                         continue;
2873                 }
2874
2875                 if (healthy_count != 0) {
2876                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
2877                                 continue;
2878                         }
2879                 }
2880
2881                 if (options.machinereadable){
2882                         printf("%d\n", i);
2883                 } else {
2884                         printf("Node %d is LVS master\n", i);
2885                 }
2886                 return 0;
2887         }
2888
2889         printf("There is no LVS master\n");
2890         return -1;
2891 }
2892
2893 /*
2894   disable monitoring on a  node
2895  */
2896 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2897 {
2898         
2899         int ret;
2900
2901         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
2902         if (ret != 0) {
2903                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
2904                 return ret;
2905         }
2906         printf("Monitoring mode:%s\n","DISABLED");
2907
2908         return 0;
2909 }
2910
2911 /*
2912   enable monitoring on a  node
2913  */
2914 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
2915 {
2916         
2917         int ret;
2918
2919         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
2920         if (ret != 0) {
2921                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
2922                 return ret;
2923         }
2924         printf("Monitoring mode:%s\n","ACTIVE");
2925
2926         return 0;
2927 }
2928
2929 /*
2930   display remote list of keys/data for a db
2931  */
2932 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
2933 {
2934         const char *db_name;
2935         struct ctdb_db_context *ctdb_db;
2936         int ret;
2937
2938         if (argc < 1) {
2939                 usage();
2940         }
2941
2942         db_name = argv[0];
2943
2944
2945         if (db_exists(ctdb, db_name)) {
2946                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2947                 return -1;
2948         }
2949
2950         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2951
2952         if (ctdb_db == NULL) {
2953                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2954                 return -1;
2955         }
2956
2957         /* traverse and dump the cluster tdb */
2958         ret = ctdb_dump_db(ctdb_db, stdout);
2959         if (ret == -1) {
2960                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
2961                 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
2962                                   " and 'ctdb getvar AllowUnhealthyDBRead'\n",
2963                                   db_name));
2964                 return -1;
2965         }
2966         talloc_free(ctdb_db);
2967
2968         printf("Dumped %d records\n", ret);
2969         return 0;
2970 }
2971
2972 /*
2973   display the content of a database key
2974  */
2975 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
2976 {
2977         const char *db_name;
2978         struct ctdb_db_context *ctdb_db;
2979         struct ctdb_record_handle *h;
2980         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2981         TDB_DATA key, data;
2982
2983         if (argc < 2) {
2984                 usage();
2985         }
2986
2987         db_name = argv[0];
2988
2989
2990         if (db_exists(ctdb, db_name)) {
2991                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
2992                 return -1;
2993         }
2994
2995         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2996
2997         if (ctdb_db == NULL) {
2998                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2999                 return -1;
3000         }
3001
3002         key.dptr  = discard_const(argv[1]);
3003         key.dsize = strlen((char *)key.dptr);
3004  
3005         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3006         if (h == NULL) {
3007                 printf("Failed to fetch record '%s' on node %d\n", 
3008                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3009                 talloc_free(tmp_ctx);
3010                 exit(10);
3011         }
3012
3013         printf("Data: size:%d ptr:[%s]\n", (int)data.dsize, data.dptr);
3014
3015         talloc_free(ctdb_db);
3016         talloc_free(tmp_ctx);
3017         return 0;
3018 }
3019
3020 /*
3021   display the content of a database key
3022  */
3023 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3024 {
3025         const char *db_name;
3026         struct ctdb_db_context *ctdb_db;
3027         struct ctdb_record_handle *h;
3028         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3029         TDB_DATA key, data;
3030
3031         if (argc < 3) {
3032                 usage();
3033         }
3034
3035         db_name = argv[0];
3036
3037
3038         if (db_exists(ctdb, db_name)) {
3039                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3040                 return -1;
3041         }
3042
3043         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3044
3045         if (ctdb_db == NULL) {
3046                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3047                 return -1;
3048         }
3049
3050         key.dptr  = discard_const(argv[1]);
3051         key.dsize = strlen((char *)key.dptr);
3052  
3053         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3054         if (h == NULL) {
3055                 printf("Failed to fetch record '%s' on node %d\n", 
3056                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3057                 talloc_free(tmp_ctx);
3058                 exit(10);
3059         }
3060
3061         data.dptr  = discard_const(argv[2]);
3062         data.dsize = strlen((char *)data.dptr);
3063  
3064         if (ctdb_record_store(h, data) != 0) {
3065                 printf("Failed to store record\n");
3066         }
3067
3068         talloc_free(h);
3069         talloc_free(ctdb_db);
3070         talloc_free(tmp_ctx);
3071         return 0;
3072 }
3073
3074 /*
3075   fetch a record from a persistent database
3076  */
3077 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3078 {
3079         const char *db_name;
3080         struct ctdb_db_context *ctdb_db;
3081         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3082         struct ctdb_transaction_handle *h;
3083         TDB_DATA key, data;
3084         int fd, ret;
3085
3086         if (argc < 2) {
3087                 talloc_free(tmp_ctx);
3088                 usage();
3089         }
3090
3091         db_name = argv[0];
3092
3093
3094         if (db_exists(ctdb, db_name)) {
3095                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
3096                 talloc_free(tmp_ctx);
3097                 return -1;
3098         }
3099
3100         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3101
3102         if (ctdb_db == NULL) {
3103                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3104                 talloc_free(tmp_ctx);
3105                 return -1;
3106         }
3107
3108         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3109         if (h == NULL) {
3110                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3111                 talloc_free(tmp_ctx);
3112                 return -1;
3113         }
3114
3115         key.dptr  = discard_const(argv[1]);
3116         key.dsize = strlen(argv[1]);
3117         ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3118         if (ret != 0) {
3119                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3120                 talloc_free(tmp_ctx);
3121                 return -1;
3122         }
3123
3124         if (data.dsize == 0 || data.dptr == NULL) {
3125                 DEBUG(DEBUG_ERR,("Record is empty\n"));
3126                 talloc_free(tmp_ctx);
3127                 return -1;
3128         }
3129
3130         if (argc == 3) {
3131           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3132                 if (fd == -1) {
3133                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3134                         talloc_free(tmp_ctx);
3135                         return -1;
3136                 }
3137                 write(fd, data.dptr, data.dsize);
3138                 close(fd);
3139         } else {
3140                 write(1, data.dptr, data.dsize);
3141         }
3142
3143         /* abort the transaction */
3144         talloc_free(h);
3145
3146
3147         talloc_free(tmp_ctx);
3148         return 0;
3149 }
3150
3151 /*
3152   fetch a record from a tdb-file
3153  */
3154 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3155 {
3156         const char *tdb_file;
3157         TDB_CONTEXT *tdb;
3158         TDB_DATA key, data;
3159         int fd;
3160
3161         if (argc < 2) {
3162                 usage();
3163         }
3164
3165         tdb_file = argv[0];
3166
3167         tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3168         if (tdb == NULL) {
3169                 DEBUG(DEBUG_ERR,("Failed to open TDB file %s\n", tdb_file));
3170                 return -1;
3171         }
3172
3173         key.dptr  = discard_const(argv[1]);
3174         key.dsize = strlen(argv[1]);
3175         data = tdb_fetch(tdb, key);
3176         if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3177                 DEBUG(DEBUG_ERR,("Failed to read record %s from tdb %s\n", argv[1], tdb_file));
3178                 tdb_close(tdb);
3179                 return -1;
3180         }
3181
3182         tdb_close(tdb);
3183
3184         if (argc == 3) {
3185           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3186                 if (fd == -1) {
3187                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3188                         return -1;
3189                 }
3190                 write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3191                 close(fd);
3192         } else {
3193                 write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
3194         }
3195
3196         return 0;
3197 }
3198
3199 /*
3200   write a record to a persistent database
3201  */
3202 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
3203 {
3204         const char *db_name;
3205         struct ctdb_db_context *ctdb_db;
3206         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3207         struct ctdb_transaction_handle *h;
3208         struct stat st;
3209         TDB_DATA key, data;
3210         int fd, ret;
3211
3212         if (argc < 3) {
3213                 talloc_free(tmp_ctx);
3214                 usage();
3215         }
3216
3217         fd = open(argv[2], O_RDONLY);
3218         if (fd == -1) {
3219                 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s  %s\n", argv[2], strerror(errno)));
3220                 talloc_free(tmp_ctx);
3221                 return -1;
3222         }
3223         
3224         ret = fstat(fd, &st);
3225         if (ret == -1) {
3226                 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
3227                 close(fd);
3228                 talloc_free(tmp_ctx);
3229                 return -1;
3230         }
3231
3232         if (!S_ISREG(st.st_mode)) {
3233                 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
3234                 close(fd);
3235                 talloc_free(tmp_ctx);
3236                 return -1;
3237         }
3238
3239         data.dsize = st.st_size;
3240         if (data.dsize == 0) {
3241                 data.dptr  = NULL;
3242         } else {
3243                 data.dptr = talloc_size(tmp_ctx, data.dsize);
3244                 if (data.dptr == NULL) {
3245                         DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
3246                         close(fd);
3247                         talloc_free(tmp_ctx);
3248                         return -1;
3249                 }
3250                 ret = read(fd, data.dptr, data.dsize);
3251                 if (ret != data.dsize) {
3252                         DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
3253                         close(fd);
3254                         talloc_free(tmp_ctx);
3255                         return -1;
3256                 }
3257         }
3258         close(fd);
3259
3260
3261         db_name = argv[0];
3262
3263         ctdb_db = ctdb_attach(ctdb, db_name, true, 0);
3264
3265         if (ctdb_db == NULL) {
3266                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3267                 talloc_free(tmp_ctx);
3268                 return -1;
3269         }
3270
3271         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3272         if (h == NULL) {
3273                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3274                 talloc_free(tmp_ctx);
3275                 return -1;
3276         }
3277
3278         key.dptr  = discard_const(argv[1]);
3279         key.dsize = strlen(argv[1]);
3280         ret = ctdb_transaction_store(h, key, data);
3281         if (ret != 0) {
3282                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
3283                 talloc_free(tmp_ctx);
3284                 return -1;
3285         }
3286
3287         ret = ctdb_transaction_commit(h);
3288         if (ret != 0) {
3289                 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
3290                 talloc_free(tmp_ctx);
3291                 return -1;
3292         }
3293
3294
3295         talloc_free(tmp_ctx);
3296         return 0;
3297 }
3298
3299 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid, 
3300                              TDB_DATA data, void *private_data)
3301 {
3302         DEBUG(DEBUG_ERR,("Log data received\n"));
3303         if (data.dsize > 0) {
3304                 printf("%s", data.dptr);
3305         }
3306
3307         exit(0);
3308 }
3309
3310 /*
3311   display a list of log messages from the in memory ringbuffer
3312  */
3313 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
3314 {
3315         int ret;
3316         int32_t res;
3317         struct ctdb_get_log_addr log_addr;
3318         TDB_DATA data;
3319         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3320         char *errmsg;
3321         struct timeval tv;
3322
3323         if (argc != 1) {
3324                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3325                 talloc_free(tmp_ctx);
3326                 return -1;
3327         }
3328
3329         log_addr.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
3330         log_addr.srvid = getpid();
3331         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3332                 log_addr.level = get_debug_by_desc(argv[0]);
3333         } else {
3334                 log_addr.level = strtol(argv[0], NULL, 0);
3335         }
3336
3337
3338         data.dptr = (unsigned char *)&log_addr;
3339         data.dsize = sizeof(log_addr);
3340
3341         DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
3342
3343         ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
3344         sleep(1);
3345
3346         DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
3347
3348         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
3349                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3350         if (ret != 0 || res != 0) {
3351                 DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
3352                 talloc_free(tmp_ctx);
3353                 return -1;
3354         }
3355
3356
3357         tv = timeval_current();
3358         /* this loop will terminate when we have received the reply */
3359         while (timeval_elapsed(&tv) < 3.0) {    
3360                 event_loop_once(ctdb->ev);
3361         }
3362
3363         DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
3364
3365         talloc_free(tmp_ctx);
3366         return 0;
3367 }
3368
3369 /*
3370   clear the in memory log area
3371  */
3372 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
3373 {
3374         int ret;
3375         int32_t res;
3376         char *errmsg;
3377         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3378
3379         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
3380                            0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
3381         if (ret != 0 || res != 0) {
3382                 DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
3383                 talloc_free(tmp_ctx);
3384                 return -1;
3385         }
3386
3387         talloc_free(tmp_ctx);
3388         return 0;
3389 }
3390
3391
3392
3393 /*
3394   display a list of the databases on a remote ctdb
3395  */
3396 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
3397 {
3398         int i, ret;
3399         struct ctdb_dbid_map *dbmap=NULL;
3400
3401         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3402         if (ret != 0) {
3403                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3404                 return ret;
3405         }
3406
3407         if(options.machinereadable){
3408                 printf(":ID:Name:Path:Persistent:Unhealthy:\n");
3409                 for(i=0;i<dbmap->num;i++){
3410                         const char *path;
3411                         const char *name;
3412                         const char *health;
3413                         bool persistent;
3414
3415                         ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
3416                                             dbmap->dbs[i].dbid, ctdb, &path);
3417                         ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
3418                                             dbmap->dbs[i].dbid, ctdb, &name);
3419                         ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
3420                                               dbmap->dbs[i].dbid, ctdb, &health);
3421                         persistent = dbmap->dbs[i].persistent;
3422                         printf(":0x%08X:%s:%s:%d:%d:\n",
3423                                dbmap->dbs[i].dbid, name, path,
3424                                !!(persistent), !!(health));
3425                 }
3426                 return 0;
3427         }
3428
3429         printf("Number of databases:%d\n", dbmap->num);
3430         for(i=0;i<dbmap->num;i++){
3431                 const char *path;
3432                 const char *name;
3433                 const char *health;
3434                 bool persistent;
3435
3436                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3437                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3438                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3439                 persistent = dbmap->dbs[i].persistent;
3440                 printf("dbid:0x%08x name:%s path:%s%s%s\n",
3441                        dbmap->dbs[i].dbid, name, path,
3442                        persistent?" PERSISTENT":"",
3443                        health?" UNHEALTHY":"");
3444         }
3445
3446         return 0;
3447 }
3448
3449 /*
3450   display the status of a database on a remote ctdb
3451  */
3452 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
3453 {
3454         int i, ret;
3455         struct ctdb_dbid_map *dbmap=NULL;
3456         const char *db_name;
3457
3458         if (argc < 1) {
3459                 usage();
3460         }
3461
3462         db_name = argv[0];
3463
3464         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
3465         if (ret != 0) {
3466                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
3467                 return ret;
3468         }
3469
3470         for(i=0;i<dbmap->num;i++){
3471                 const char *path;
3472                 const char *name;
3473                 const char *health;
3474                 bool persistent;
3475
3476                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
3477                 if (strcmp(name, db_name) != 0) {
3478                         continue;
3479                 }
3480
3481                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
3482                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
3483                 persistent = dbmap->dbs[i].persistent;
3484                 printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nHEALTH: %s\n",
3485                        dbmap->dbs[i].dbid, name, path,
3486                        persistent?"yes":"no",
3487                        health?health:"OK");
3488                 return 0;
3489         }
3490
3491         DEBUG(DEBUG_ERR, ("db %s doesn't exist on node %u\n", db_name, options.pnn));
3492         return 0;
3493 }
3494
3495 /*
3496   check if the local node is recmaster or not
3497   it will return 1 if this node is the recmaster and 0 if it is not
3498   or if the local ctdb daemon could not be contacted
3499  */
3500 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3501 {
3502         uint32_t mypnn, recmaster;
3503         int ret;
3504
3505         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
3506         if (mypnn == -1) {
3507                 printf("Failed to get pnn of node\n");
3508                 return 1;
3509         }
3510
3511         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
3512         if (ret != 0) {
3513                 printf("Failed to get the recmaster\n");
3514                 return 1;
3515         }
3516
3517         if (recmaster != mypnn) {
3518                 printf("this node is not the recmaster\n");
3519                 return 1;
3520         }
3521
3522         printf("this node is the recmaster\n");
3523         return 0;
3524 }
3525
3526 /*
3527   ping a node
3528  */
3529 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
3530 {
3531         int ret;
3532         struct timeval tv = timeval_current();
3533         ret = ctdb_ctrl_ping(ctdb, options.pnn);
3534         if (ret == -1) {
3535                 printf("Unable to get ping response from node %u\n", options.pnn);
3536                 return -1;
3537         } else {
3538                 printf("response from %u time=%.6f sec  (%d clients)\n", 
3539                        options.pnn, timeval_elapsed(&tv), ret);
3540         }
3541         return 0;
3542 }
3543
3544
3545 /*
3546   get a tunable
3547  */
3548 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
3549 {
3550         const char *name;
3551         uint32_t value;
3552         int ret;
3553
3554         if (argc < 1) {
3555                 usage();
3556         }
3557
3558         name = argv[0];
3559         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
3560         if (ret == -1) {
3561                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
3562                 return -1;
3563         }
3564
3565         printf("%-19s = %u\n", name, value);
3566         return 0;
3567 }
3568
3569 /*
3570   set a tunable
3571  */
3572 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
3573 {
3574         const char *name;
3575         uint32_t value;
3576         int ret;
3577
3578         if (argc < 2) {
3579                 usage();
3580         }
3581
3582         name = argv[0];
3583         value = strtoul(argv[1], NULL, 0);
3584
3585         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
3586         if (ret == -1) {
3587                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
3588                 return -1;
3589         }
3590         return 0;
3591 }
3592
3593 /*
3594   list all tunables
3595  */
3596 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
3597 {
3598         uint32_t count;
3599         const char **list;
3600         int ret, i;
3601
3602         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
3603         if (ret == -1) {
3604                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
3605                 return -1;
3606         }
3607
3608         for (i=0;i<count;i++) {
3609                 control_getvar(ctdb, 1, &list[i]);
3610         }
3611
3612         talloc_free(list);
3613         
3614         return 0;
3615 }
3616
3617 /*
3618   display debug level on a node
3619  */
3620 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3621 {
3622         int ret;
3623         int32_t level;
3624
3625         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
3626         if (ret != 0) {
3627                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
3628                 return ret;
3629         } else {
3630                 if (options.machinereadable){
3631                         printf(":Name:Level:\n");
3632                         printf(":%s:%d:\n",get_debug_by_level(level),level);
3633                 } else {
3634                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
3635                 }
3636         }
3637         return 0;
3638 }
3639
3640 /*
3641   display reclock file of a node
3642  */
3643 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3644 {
3645         int ret;
3646         const char *reclock;
3647
3648         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
3649         if (ret != 0) {
3650                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3651                 return ret;
3652         } else {
3653                 if (options.machinereadable){
3654                         if (reclock != NULL) {
3655                                 printf("%s", reclock);
3656                         }
3657                 } else {
3658                         if (reclock == NULL) {
3659                                 printf("No reclock file used.\n");
3660                         } else {
3661                                 printf("Reclock file:%s\n", reclock);
3662                         }
3663                 }
3664         }
3665         return 0;
3666 }
3667
3668 /*
3669   set the reclock file of a node
3670  */
3671 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
3672 {
3673         int ret;
3674         const char *reclock;
3675
3676         if (argc == 0) {
3677                 reclock = NULL;
3678         } else if (argc == 1) {
3679                 reclock = argv[0];
3680         } else {
3681                 usage();
3682         }
3683
3684         ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
3685         if (ret != 0) {
3686                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
3687                 return ret;
3688         }
3689         return 0;
3690 }
3691
3692 /*
3693   set the natgw state on/off
3694  */
3695 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
3696 {
3697         int ret;
3698         uint32_t natgwstate;
3699
3700         if (argc == 0) {
3701                 usage();
3702         }
3703
3704         if (!strcmp(argv[0], "on")) {
3705                 natgwstate = 1;
3706         } else if (!strcmp(argv[0], "off")) {
3707                 natgwstate = 0;
3708         } else {
3709                 usage();
3710         }
3711
3712         ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
3713         if (ret != 0) {
3714                 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
3715                 return ret;
3716         }
3717
3718         return 0;
3719 }
3720
3721 /*
3722   set the lmaster role on/off
3723  */
3724 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3725 {
3726         int ret;
3727         uint32_t lmasterrole;
3728
3729         if (argc == 0) {
3730                 usage();
3731         }
3732
3733         if (!strcmp(argv[0], "on")) {
3734                 lmasterrole = 1;
3735         } else if (!strcmp(argv[0], "off")) {
3736                 lmasterrole = 0;
3737         } else {
3738                 usage();
3739         }
3740
3741         ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
3742         if (ret != 0) {
3743                 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
3744                 return ret;
3745         }
3746
3747         return 0;
3748 }
3749
3750 /*
3751   set the recmaster role on/off
3752  */
3753 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
3754 {
3755         int ret;
3756         uint32_t recmasterrole;
3757
3758         if (argc == 0) {
3759                 usage();
3760         }
3761
3762         if (!strcmp(argv[0], "on")) {
3763                 recmasterrole = 1;
3764         } else if (!strcmp(argv[0], "off")) {
3765                 recmasterrole = 0;
3766         } else {
3767                 usage();
3768         }
3769
3770         ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
3771         if (ret != 0) {
3772                 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
3773                 return ret;
3774         }
3775
3776         return 0;
3777 }
3778
3779 /*
3780   set debug level on a node or all nodes
3781  */
3782 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
3783 {
3784         int i, ret;
3785         int32_t level;
3786
3787         if (argc == 0) {
3788                 printf("You must specify the debug level. Valid levels are:\n");
3789                 for (i=0; debug_levels[i].description != NULL; i++) {
3790                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3791                 }
3792
3793                 return 0;
3794         }
3795
3796         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
3797                 level = get_debug_by_desc(argv[0]);
3798         } else {
3799                 level = strtol(argv[0], NULL, 0);
3800         }
3801
3802         for (i=0; debug_levels[i].description != NULL; i++) {
3803                 if (level == debug_levels[i].level) {
3804                         break;
3805                 }
3806         }
3807         if (debug_levels[i].description == NULL) {
3808                 printf("Invalid debug level, must be one of\n");
3809                 for (i=0; debug_levels[i].description != NULL; i++) {
3810                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
3811                 }
3812                 return -1;
3813         }
3814
3815         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
3816         if (ret != 0) {
3817                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
3818         }
3819         return 0;
3820 }
3821
3822
3823 /*
3824   thaw a node
3825  */
3826 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
3827 {
3828         int ret;
3829         uint32_t priority;
3830         
3831         if (argc == 1) {
3832                 priority = strtol(argv[0], NULL, 0);
3833         } else {
3834                 priority = 0;
3835         }
3836         DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
3837
3838         ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
3839         if (ret != 0) {
3840                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
3841         }               
3842         return 0;
3843 }
3844
3845
3846 /*
3847   attach to a database
3848  */
3849 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
3850 {
3851         const char *db_name;
3852         struct ctdb_db_context *ctdb_db;
3853
3854         if (argc < 1) {
3855                 usage();
3856         }
3857         db_name = argv[0];
3858
3859         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
3860         if (ctdb_db == NULL) {
3861                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3862                 return -1;
3863         }
3864
3865         return 0;
3866 }
3867
3868 /*
3869   set db priority
3870  */
3871 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3872 {
3873         struct ctdb_db_priority db_prio;
3874         int ret;
3875
3876         if (argc < 2) {
3877                 usage();
3878         }
3879
3880         db_prio.db_id    = strtoul(argv[0], NULL, 0);
3881         db_prio.priority = strtoul(argv[1], NULL, 0);
3882
3883         ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
3884         if (ret != 0) {
3885                 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
3886                 return -1;
3887         }
3888
3889         return 0;
3890 }
3891
3892 /*
3893   get db priority
3894  */
3895 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
3896 {
3897         uint32_t db_id, priority;
3898         int ret;
3899
3900         if (argc < 1) {
3901                 usage();
3902         }
3903
3904         db_id = strtoul(argv[0], NULL, 0);
3905
3906         ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
3907         if (ret != 0) {
3908                 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
3909                 return -1;
3910         }
3911
3912         DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
3913
3914         return 0;
3915 }
3916
3917 /*
3918   run an eventscript on a node
3919  */
3920 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
3921 {
3922         TDB_DATA data;
3923         int ret;
3924         int32_t res;
3925         char *errmsg;
3926         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3927
3928         if (argc != 1) {
3929                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
3930                 return -1;
3931         }
3932
3933         data.dptr = (unsigned char *)discard_const(argv[0]);
3934         data.dsize = strlen((char *)data.dptr) + 1;
3935
3936         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
3937
3938         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
3939                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
3940         if (ret != 0 || res != 0) {
3941                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
3942                 talloc_free(tmp_ctx);
3943                 return -1;
3944         }
3945         talloc_free(tmp_ctx);
3946         return 0;
3947 }
3948
3949 #define DB_VERSION 1
3950 #define MAX_DB_NAME 64
3951 struct db_file_header {
3952         unsigned long version;
3953         time_t timestamp;
3954         unsigned long persistent;
3955         unsigned long size;
3956         const char name[MAX_DB_NAME];
3957 };
3958
3959 struct backup_data {
3960         struct ctdb_marshall_buffer *records;
3961         uint32_t len;
3962         uint32_t total;
3963         bool traverse_error;
3964 };
3965
3966 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
3967 {
3968         struct backup_data *bd = talloc_get_type(private, struct backup_data);
3969         struct ctdb_rec_data *rec;
3970
3971         /* add the record */
3972         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
3973         if (rec == NULL) {
3974                 bd->traverse_error = true;
3975                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
3976                 return -1;
3977         }
3978         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
3979         if (bd->records == NULL) {
3980                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
3981                 bd->traverse_error = true;
3982                 return -1;
3983         }
3984         bd->records->count++;
3985         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
3986         bd->len += rec->length;
3987         talloc_free(rec);
3988
3989         bd->total++;
3990         return 0;
3991 }
3992
3993 /*
3994  * backup a database to a file 
3995  */
3996 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
3997 {
3998         int i, ret;
3999         struct ctdb_dbid_map *dbmap=NULL;
4000         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4001         struct db_file_header dbhdr;
4002         struct ctdb_db_context *ctdb_db;
4003         struct backup_data *bd;
4004         int fh = -1;
4005         int status = -1;
4006         const char *reason = NULL;
4007
4008         if (argc != 2) {
4009                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4010                 return -1;
4011         }
4012
4013         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
4014         if (ret != 0) {
4015                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4016                 return ret;
4017         }
4018
4019         for(i=0;i<dbmap->num;i++){
4020                 const char *name;
4021
4022                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
4023                 if(!strcmp(argv[0], name)){
4024                         talloc_free(discard_const(name));
4025                         break;
4026                 }
4027                 talloc_free(discard_const(name));
4028         }
4029         if (i == dbmap->num) {
4030                 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
4031                 talloc_free(tmp_ctx);
4032                 return -1;
4033         }
4034
4035         ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4036                                     dbmap->dbs[i].dbid, tmp_ctx, &reason);
4037         if (ret != 0) {
4038                 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
4039                                  argv[0]));
4040                 talloc_free(tmp_ctx);
4041                 return -1;
4042         }
4043         if (reason) {
4044                 uint32_t allow_unhealthy = 0;
4045
4046                 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
4047                                       "AllowUnhealthyDBRead",
4048                                       &allow_unhealthy);
4049
4050                 if (allow_unhealthy != 1) {
4051                         DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
4052                                          argv[0], reason));
4053
4054                         DEBUG(DEBUG_ERR,("disallow backup : tunnable AllowUnhealthyDBRead = %u\n",
4055                                          allow_unhealthy));
4056                         talloc_free(tmp_ctx);
4057                         return -1;
4058                 }
4059
4060                 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
4061                                      argv[0], argv[0]));
4062                 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
4063                                      "tunnable AllowUnhealthyDBRead = %u\n",
4064                                      allow_unhealthy));
4065         }
4066
4067         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4068         if (ctdb_db == NULL) {
4069                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
4070                 talloc_free(tmp_ctx);
4071                 return -1;
4072         }
4073
4074
4075         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
4076         if (ret == -1) {
4077                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
4078                 talloc_free(tmp_ctx);
4079                 return -1;
4080         }
4081
4082
4083         bd = talloc_zero(tmp_ctx, struct backup_data);
4084         if (bd == NULL) {
4085                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
4086                 talloc_free(tmp_ctx);
4087                 return -1;
4088         }
4089
4090         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
4091         if (bd->records == NULL) {
4092                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
4093                 talloc_free(tmp_ctx);
4094                 return -1;
4095         }
4096
4097         bd->len = offsetof(struct ctdb_marshall_buffer, data);
4098         bd->records->db_id = ctdb_db->db_id;
4099         /* traverse the database collecting all records */
4100         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
4101             bd->traverse_error) {
4102                 DEBUG(DEBUG_ERR,("Traverse error\n"));
4103                 talloc_free(tmp_ctx);
4104                 return -1;              
4105         }
4106
4107         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
4108
4109
4110         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
4111         if (fh == -1) {
4112                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
4113                 talloc_free(tmp_ctx);
4114                 return -1;
4115         }
4116
4117         dbhdr.version = DB_VERSION;
4118         dbhdr.timestamp = time(NULL);
4119         dbhdr.persistent = dbmap->dbs[i].persistent;
4120         dbhdr.size = bd->len;
4121         if (strlen(argv[0]) >= MAX_DB_NAME) {
4122                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
4123                 goto done;
4124         }
4125         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
4126         ret = write(fh, &dbhdr, sizeof(dbhdr));
4127         if (ret == -1) {
4128                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4129                 goto done;
4130         }
4131         ret = write(fh, bd->records, bd->len);
4132         if (ret == -1) {
4133                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
4134                 goto done;
4135         }
4136
4137         status = 0;
4138 done:
4139         if (fh != -1) {
4140                 ret = close(fh);
4141                 if (ret == -1) {
4142                         DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
4143                 }
4144         }
4145         talloc_free(tmp_ctx);
4146         return status;
4147 }
4148
4149 /*
4150  * restore a database from a file 
4151  */
4152 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
4153 {
4154         int ret;
4155         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4156         TDB_DATA outdata;
4157         TDB_DATA data;
4158         struct db_file_header dbhdr;
4159         struct ctdb_db_context *ctdb_db;
4160         struct ctdb_node_map *nodemap=NULL;
4161         struct ctdb_vnn_map *vnnmap=NULL;
4162         int i, fh;
4163         struct ctdb_control_wipe_database w;
4164         uint32_t *nodes;
4165         uint32_t generation;
4166         struct tm *tm;
4167         char tbuf[100];
4168         char *dbname;
4169
4170         if (argc < 1 || argc > 2) {
4171                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4172                 return -1;
4173         }
4174
4175         fh = open(argv[0], O_RDONLY);
4176         if (fh == -1) {
4177                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4178                 talloc_free(tmp_ctx);
4179                 return -1;
4180         }
4181
4182         read(fh, &dbhdr, sizeof(dbhdr));
4183         if (dbhdr.version != DB_VERSION) {
4184                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4185                 talloc_free(tmp_ctx);
4186                 return -1;
4187         }
4188
4189         dbname = discard_const(dbhdr.name);
4190         if (argc == 2) {
4191                 dbname = discard_const(argv[1]);
4192         }
4193
4194         outdata.dsize = dbhdr.size;
4195         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4196         if (outdata.dptr == NULL) {
4197                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4198                 close(fh);
4199                 talloc_free(tmp_ctx);
4200                 return -1;
4201         }               
4202         read(fh, outdata.dptr, outdata.dsize);
4203         close(fh);
4204
4205         tm = localtime(&dbhdr.timestamp);
4206         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4207         printf("Restoring database '%s' from backup @ %s\n",
4208                 dbname, tbuf);
4209
4210
4211         ctdb_db = ctdb_attach(ctdb, dbname, dbhdr.persistent, 0);
4212         if (ctdb_db == NULL) {
4213                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
4214                 talloc_free(tmp_ctx);
4215                 return -1;
4216         }
4217
4218         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
4219         if (ret != 0) {
4220                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
4221                 talloc_free(tmp_ctx);
4222                 return ret;
4223         }
4224
4225
4226         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
4227         if (ret != 0) {
4228                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
4229                 talloc_free(tmp_ctx);
4230                 return ret;
4231         }
4232
4233         /* freeze all nodes */
4234         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4235         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4236                 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4237                                         nodes, i,
4238                                         TIMELIMIT(),
4239                                         false, tdb_null,
4240                                         NULL, NULL,
4241                                         NULL) != 0) {
4242                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4243                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4244                         talloc_free(tmp_ctx);
4245                         return -1;
4246                 }
4247         }
4248
4249         generation = vnnmap->generation;
4250         data.dptr = (void *)&generation;
4251         data.dsize = sizeof(generation);
4252
4253         /* start a cluster wide transaction */
4254         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4255         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4256                                         nodes, 0,
4257                                         TIMELIMIT(), false, data,
4258                                         NULL, NULL,
4259                                         NULL) != 0) {
4260                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
4261                 return -1;
4262         }
4263
4264
4265         w.db_id = ctdb_db->db_id;
4266         w.transaction_id = generation;
4267
4268         data.dptr = (void *)&w;
4269         data.dsize = sizeof(w);
4270
4271         /* wipe all the remote databases. */
4272         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4273         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4274                                         nodes, 0,
4275                                         TIMELIMIT(), false, data,
4276                                         NULL, NULL,
4277                                         NULL) != 0) {
4278                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4279                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4280                 talloc_free(tmp_ctx);
4281                 return -1;
4282         }
4283         
4284         /* push the database */
4285         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4286         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
4287                                         nodes, 0,
4288                                         TIMELIMIT(), false, outdata,
4289                                         NULL, NULL,
4290                                         NULL) != 0) {
4291                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
4292                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4293                 talloc_free(tmp_ctx);
4294                 return -1;
4295         }
4296
4297         data.dptr = (void *)&ctdb_db->db_id;
4298         data.dsize = sizeof(ctdb_db->db_id);
4299
4300         /* mark the database as healthy */
4301         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4302         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4303                                         nodes, 0,
4304                                         TIMELIMIT(), false, data,
4305                                         NULL, NULL,
4306                                         NULL) != 0) {
4307                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4308                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4309                 talloc_free(tmp_ctx);
4310                 return -1;
4311         }
4312
4313         data.dptr = (void *)&generation;
4314         data.dsize = sizeof(generation);
4315
4316         /* commit all the changes */
4317         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4318                                         nodes, 0,
4319                                         TIMELIMIT(), false, data,
4320                                         NULL, NULL,
4321                                         NULL) != 0) {
4322                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4323                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4324                 talloc_free(tmp_ctx);
4325                 return -1;
4326         }
4327
4328
4329         /* thaw all nodes */
4330         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4331         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4332                                         nodes, 0,
4333                                         TIMELIMIT(),
4334                                         false, tdb_null,
4335                                         NULL, NULL,
4336                                         NULL) != 0) {
4337                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4338                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4339                 talloc_free(tmp_ctx);
4340                 return -1;
4341         }
4342
4343
4344         talloc_free(tmp_ctx);
4345         return 0;
4346 }
4347
4348 /*
4349  * dump a database backup from a file
4350  */
4351 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
4352 {
4353         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4354         TDB_DATA outdata;
4355         struct db_file_header dbhdr;
4356         int i, fh;
4357         struct tm *tm;
4358         char tbuf[100];
4359         struct ctdb_rec_data *rec = NULL;
4360         struct ctdb_marshall_buffer *m;
4361
4362         if (argc != 1) {
4363                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4364                 return -1;
4365         }
4366
4367         fh = open(argv[0], O_RDONLY);
4368         if (fh == -1) {
4369                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
4370                 talloc_free(tmp_ctx);
4371                 return -1;
4372         }
4373
4374         read(fh, &dbhdr, sizeof(dbhdr));
4375         if (dbhdr.version != DB_VERSION) {
4376                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
4377                 talloc_free(tmp_ctx);
4378                 return -1;
4379         }
4380
4381         outdata.dsize = dbhdr.size;
4382         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
4383         if (outdata.dptr == NULL) {
4384                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
4385                 close(fh);
4386                 talloc_free(tmp_ctx);
4387                 return -1;
4388         }
4389         read(fh, outdata.dptr, outdata.dsize);
4390         close(fh);
4391         m = (struct ctdb_marshall_buffer *)outdata.dptr;
4392
4393         tm = localtime(&dbhdr.timestamp);
4394         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
4395         printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
4396                 dbhdr.name, m->db_id, tbuf);
4397
4398         for (i=0; i < m->count; i++) {
4399                 uint32_t reqid = 0;
4400                 TDB_DATA key, data;
4401
4402                 /* we do not want the header splitted, so we pass NULL*/
4403                 rec = ctdb_marshall_loop_next(m, rec, &reqid,
4404                                               NULL, &key, &data);
4405
4406                 ctdb_dumpdb_record(ctdb, key, data, stdout);
4407         }
4408
4409         printf("Dumped %d records\n", i);
4410         talloc_free(tmp_ctx);
4411         return 0;
4412 }
4413
4414 /*
4415  * wipe a database from a file
4416  */
4417 static int control_wipedb(struct ctdb_context *ctdb, int argc,
4418                           const char **argv)
4419 {
4420         int ret;
4421         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4422         TDB_DATA data;
4423         struct ctdb_db_context *ctdb_db;
4424         struct ctdb_node_map *nodemap = NULL;
4425         struct ctdb_vnn_map *vnnmap = NULL;
4426         int i;
4427         struct ctdb_control_wipe_database w;
4428         uint32_t *nodes;
4429         uint32_t generation;
4430         struct ctdb_dbid_map *dbmap = NULL;
4431
4432         if (argc != 1) {
4433                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
4434                 return -1;
4435         }
4436
4437         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4438                                  &dbmap);
4439         if (ret != 0) {
4440                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n",
4441                                   options.pnn));
4442                 return ret;
4443         }
4444
4445         for(i=0;i<dbmap->num;i++){
4446                 const char *name;
4447
4448                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4449                                     dbmap->dbs[i].dbid, tmp_ctx, &name);
4450                 if(!strcmp(argv[0], name)){
4451                         talloc_free(discard_const(name));
4452                         break;
4453                 }
4454                 talloc_free(discard_const(name));
4455         }
4456         if (i == dbmap->num) {
4457                 DEBUG(DEBUG_ERR, ("No database with name '%s' found\n",
4458                                   argv[0]));
4459                 talloc_free(tmp_ctx);
4460                 return -1;
4461         }
4462
4463         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
4464         if (ctdb_db == NULL) {
4465                 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
4466                                   argv[0]));
4467                 talloc_free(tmp_ctx);
4468                 return -1;
4469         }
4470
4471         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
4472                                    &nodemap);
4473         if (ret != 0) {
4474                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
4475                                   options.pnn));
4476                 talloc_free(tmp_ctx);
4477                 return ret;
4478         }
4479
4480         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
4481                                   &vnnmap);
4482         if (ret != 0) {
4483                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
4484                                   options.pnn));
4485                 talloc_free(tmp_ctx);
4486                 return ret;
4487         }
4488
4489         /* freeze all nodes */
4490         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4491         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
4492                 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
4493                                                 nodes, i,
4494                                                 TIMELIMIT(),
4495                                                 false, tdb_null,
4496                                                 NULL, NULL,
4497                                                 NULL);
4498                 if (ret != 0) {
4499                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
4500                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
4501                                              CTDB_RECOVERY_ACTIVE);
4502                         talloc_free(tmp_ctx);
4503                         return -1;
4504                 }
4505         }
4506
4507         generation = vnnmap->generation;
4508         data.dptr = (void *)&generation;
4509         data.dsize = sizeof(generation);
4510
4511         /* start a cluster wide transaction */
4512         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4513         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
4514                                         nodes, 0,
4515                                         TIMELIMIT(), false, data,
4516                                         NULL, NULL,
4517                                         NULL);
4518         if (ret!= 0) {
4519                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
4520                                   "transactions.\n"));
4521                 return -1;
4522         }
4523
4524         w.db_id = ctdb_db->db_id;
4525         w.transaction_id = generation;
4526
4527         data.dptr = (void *)&w;
4528         data.dsize = sizeof(w);
4529
4530         /* wipe all the remote databases. */
4531         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4532         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
4533                                         nodes, 0,
4534                                         TIMELIMIT(), false, data,
4535                                         NULL, NULL,
4536                                         NULL) != 0) {
4537                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
4538                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4539                 talloc_free(tmp_ctx);
4540                 return -1;
4541         }
4542
4543         data.dptr = (void *)&ctdb_db->db_id;
4544         data.dsize = sizeof(ctdb_db->db_id);
4545
4546         /* mark the database as healthy */
4547         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4548         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
4549                                         nodes, 0,
4550                                         TIMELIMIT(), false, data,
4551                                         NULL, NULL,
4552                                         NULL) != 0) {
4553                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
4554                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4555                 talloc_free(tmp_ctx);
4556                 return -1;
4557         }
4558
4559         data.dptr = (void *)&generation;
4560         data.dsize = sizeof(generation);
4561
4562         /* commit all the changes */
4563         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
4564                                         nodes, 0,
4565                                         TIMELIMIT(), false, data,
4566                                         NULL, NULL,
4567                                         NULL) != 0) {
4568                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
4569                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4570                 talloc_free(tmp_ctx);
4571                 return -1;
4572         }
4573
4574         /* thaw all nodes */
4575         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
4576         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
4577                                         nodes, 0,
4578                                         TIMELIMIT(),
4579                                         false, tdb_null,
4580                                         NULL, NULL,
4581                                         NULL) != 0) {
4582                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
4583                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
4584                 talloc_free(tmp_ctx);
4585                 return -1;
4586         }
4587
4588         talloc_free(tmp_ctx);
4589         return 0;
4590 }
4591
4592 /*
4593   dump memory usage
4594  */
4595 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4596 {
4597         TDB_DATA data;
4598         int ret;
4599         int32_t res;
4600         char *errmsg;
4601         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4602         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
4603                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
4604         if (ret != 0 || res != 0) {
4605                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
4606                 talloc_free(tmp_ctx);
4607                 return -1;
4608         }
4609         write(1, data.dptr, data.dsize);
4610         talloc_free(tmp_ctx);
4611         return 0;
4612 }
4613
4614 /*
4615   handler for memory dumps
4616 */
4617 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4618                              TDB_DATA data, void *private_data)
4619 {
4620         write(1, data.dptr, data.dsize);
4621         exit(0);
4622 }
4623
4624 /*
4625   dump memory usage on the recovery daemon
4626  */
4627 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
4628 {
4629         int ret;
4630         TDB_DATA data;
4631         struct rd_memdump_reply rd;
4632
4633         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4634         if (rd.pnn == -1) {
4635                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
4636                 return -1;
4637         }
4638         rd.srvid = getpid();
4639
4640         /* register a message port for receiveing the reply so that we
4641            can receive the reply
4642         */
4643         ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
4644
4645
4646         data.dptr = (uint8_t *)&rd;
4647         data.dsize = sizeof(rd);
4648
4649         ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
4650         if (ret != 0) {
4651                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4652                 return -1;
4653         }
4654
4655         /* this loop will terminate when we have received the reply */
4656         while (1) {     
4657                 event_loop_once(ctdb->ev);
4658         }
4659
4660         return 0;
4661 }
4662
4663 /*
4664   send a message to a srvid
4665  */
4666 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
4667 {
4668         unsigned long srvid;
4669         int ret;
4670         TDB_DATA data;
4671
4672         if (argc < 2) {
4673                 usage();
4674         }
4675
4676         srvid      = strtoul(argv[0], NULL, 0);
4677
4678         data.dptr = (uint8_t *)discard_const(argv[1]);
4679         data.dsize= strlen(argv[1]);
4680
4681         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
4682         if (ret != 0) {
4683                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
4684                 return -1;
4685         }
4686
4687         return 0;
4688 }
4689
4690 /*
4691   handler for msglisten
4692 */
4693 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4694                              TDB_DATA data, void *private_data)
4695 {
4696         int i;
4697
4698         printf("Message received: ");
4699         for (i=0;i<data.dsize;i++) {
4700                 printf("%c", data.dptr[i]);
4701         }
4702         printf("\n");
4703 }
4704
4705 /*
4706   listen for messages on a messageport
4707  */
4708 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
4709 {
4710         uint64_t srvid;
4711
4712         srvid = getpid();
4713
4714         /* register a message port and listen for messages
4715         */
4716         ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
4717         printf("Listening for messages on srvid:%d\n", (int)srvid);
4718
4719         while (1) {     
4720                 event_loop_once(ctdb->ev);
4721         }
4722
4723         return 0;
4724 }
4725
4726 /*
4727   list all nodes in the cluster
4728   we parse the nodes file directly
4729  */
4730 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
4731 {
4732         TALLOC_CTX *mem_ctx = talloc_new(NULL);
4733         struct pnn_node *pnn_nodes;
4734         struct pnn_node *pnn_node;
4735
4736         pnn_nodes = read_nodes_file(mem_ctx);
4737         if (pnn_nodes == NULL) {
4738                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
4739                 talloc_free(mem_ctx);
4740                 return -1;
4741         }
4742
4743         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
4744                 ctdb_sock_addr addr;
4745                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
4746                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
4747                         talloc_free(mem_ctx);
4748                         return -1;
4749                 }
4750                 if (options.machinereadable){
4751                         printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
4752                 } else {
4753                         printf("%s\n", pnn_node->addr);
4754                 }
4755         }
4756         talloc_free(mem_ctx);
4757
4758         return 0;
4759 }
4760
4761 /*
4762   reload the nodes file on the local node
4763  */
4764 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
4765 {
4766         int i, ret;
4767         int mypnn;
4768         struct ctdb_node_map *nodemap=NULL;
4769
4770         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
4771         if (mypnn == -1) {
4772                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
4773                 return -1;
4774         }
4775
4776         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
4777         if (ret != 0) {
4778                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
4779                 return ret;
4780         }
4781
4782         /* reload the nodes file on all remote nodes */
4783         for (i=0;i<nodemap->num;i++) {
4784                 if (nodemap->nodes[i].pnn == mypnn) {
4785                         continue;
4786                 }
4787                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
4788                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
4789                         nodemap->nodes[i].pnn);
4790                 if (ret != 0) {
4791                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
4792                 }
4793         }
4794
4795         /* reload the nodes file on the local node */
4796         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
4797         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
4798         if (ret != 0) {
4799                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
4800         }
4801
4802         /* initiate a recovery */
4803         control_recover(ctdb, argc, argv);
4804
4805         return 0;
4806 }
4807
4808
4809 static const struct {
4810         const char *name;
4811         int (*fn)(struct ctdb_context *, int, const char **);
4812         bool auto_all;
4813         bool without_daemon; /* can be run without daemon running ? */
4814         const char *msg;
4815         const char *args;
4816 } ctdb_commands[] = {
4817 #ifdef CTDB_VERS
4818         { "version",         control_version,           true,   false,  "show version of ctdb" },
4819 #endif
4820         { "status",          control_status,            true,   false,  "show node status" },
4821         { "uptime",          control_uptime,            true,   false,  "show node uptime" },
4822         { "ping",            control_ping,              true,   false,  "ping all nodes" },
4823         { "getvar",          control_getvar,            true,   false,  "get a tunable variable",               "<name>"},
4824         { "setvar",          control_setvar,            true,   false,  "set a tunable variable",               "<name> <value>"},
4825         { "listvars",        control_listvars,          true,   false,  "list tunable variables"},
4826         { "statistics",      control_statistics,        false,  false, "show statistics" },
4827         { "statisticsreset", control_statistics_reset,  true,   false,  "reset statistics"},
4828         { "stats",           control_stats,             false,  false,  "show rolling statistics", "[number of history records]" },
4829         { "ip",              control_ip,                false,  false,  "show which public ip's that ctdb manages" },
4830         { "ipinfo",          control_ipinfo,            true,   false,  "show details about a public ip that ctdb manages", "<ip>" },
4831         { "ifaces",          control_ifaces,            true,   false,  "show which interfaces that ctdb manages" },
4832         { "setifacelink",    control_setifacelink,      true,   false,  "set interface link status", "<iface> <status>" },
4833         { "process-exists",  control_process_exists,    true,   false,  "check if a process exists on a node",  "<pid>"},
4834         { "getdbmap",        control_getdbmap,          true,   false,  "show the database map" },
4835         { "getdbstatus",     control_getdbstatus,       true,   false,  "show the status of a database", "<dbname>" },
4836         { "catdb",           control_catdb,             true,   false,  "dump a database" ,                     "<dbname>"},
4837         { "getmonmode",      control_getmonmode,        true,   false,  "show monitoring mode" },
4838         { "getcapabilities", control_getcapabilities,   true,   false,  "show node capabilities" },
4839         { "pnn",             control_pnn,               true,   false,  "show the pnn of the currnet node" },
4840         { "lvs",             control_lvs,               true,   false,  "show lvs configuration" },
4841         { "lvsmaster",       control_lvsmaster,         true,   false,  "show which node is the lvs master" },
4842         { "disablemonitor",      control_disable_monmode,true,  false,  "set monitoring mode to DISABLE" },
4843         { "enablemonitor",      control_enable_monmode, true,   false,  "set monitoring mode to ACTIVE" },
4844         { "setdebug",        control_setdebug,          true,   false,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
4845         { "getdebug",        control_getdebug,          true,   false,  "get debug level" },
4846         { "getlog",          control_getlog,            true,   false,  "get the log data from the in memory ringbuffer", "<level>" },
4847         { "clearlog",          control_clearlog,        true,   false,  "clear the log data from the in memory ringbuffer" },
4848         { "attach",          control_attach,            true,   false,  "attach to a database",                 "<dbname>" },
4849         { "dumpmemory",      control_dumpmemory,        true,   false,  "dump memory map to stdout" },
4850         { "rddumpmemory",    control_rddumpmemory,      true,   false,  "dump memory map from the recovery daemon to stdout" },
4851         { "getpid",          control_getpid,            true,   false,  "get ctdbd process ID" },
4852         { "disable",         control_disable,           true,   false,  "disable a nodes public IP" },
4853         { "enable",          control_enable,            true,   false,  "enable a nodes public IP" },
4854         { "stop",            control_stop,              true,   false,  "stop a node" },
4855         { "continue",        control_continue,          true,   false,  "re-start a stopped node" },
4856         { "ban",             control_ban,               true,   false,  "ban a node from the cluster",          "<bantime|0>"},
4857         { "unban",           control_unban,             true,   false,  "unban a node" },
4858         { "showban",         control_showban,           true,   false,  "show ban information"},
4859         { "shutdown",        control_shutdown,          true,   false,  "shutdown ctdbd" },
4860         { "recover",         control_recover,           true,   false,  "force recovery" },
4861         { "sync",            control_ipreallocate,      true,   false,  "wait until ctdbd has synced all state changes" },
4862         { "ipreallocate",    control_ipreallocate,      true,   false,  "force the recovery daemon to perform a ip reallocation procedure" },
4863         { "thaw",            control_thaw,              true,   false,  "thaw databases", "[priority:1-3]" },
4864         { "isnotrecmaster",  control_isnotrecmaster,    false,  false,  "check if the local node is recmaster or not" },
4865         { "killtcp",         kill_tcp,                  false,  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
4866         { "gratiousarp",     control_gratious_arp,      false,  false, "send a gratious arp", "<ip> <interface>" },
4867         { "tickle",          tickle_tcp,                false,  false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
4868         { "gettickles",      control_get_tickles,       false,  false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
4869         { "addtickle",       control_add_tickle,        false,  false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
4870
4871         { "deltickle",       control_del_tickle,        false,  false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
4872
4873         { "regsrvid",        regsrvid,                  false,  false, "register a server id", "<pnn> <type> <id>" },
4874         { "unregsrvid",      unregsrvid,                false,  false, "unregister a server id", "<pnn> <type> <id>" },
4875         { "chksrvid",        chksrvid,                  false,  false, "check if a server id exists", "<pnn> <type> <id>" },
4876         { "getsrvids",       getsrvids,                 false,  false, "get a list of all server ids"},
4877         { "vacuum",          ctdb_vacuum,               false,  false, "vacuum the databases of empty records", "[max_records]"},
4878         { "repack",          ctdb_repack,               false,  false, "repack all databases", "[max_freelist]"},
4879         { "listnodes",       control_listnodes,         false,  true, "list all nodes in the cluster"},
4880         { "reloadnodes",     control_reload_nodes_file, false,  false, "reload the nodes file and restart the transport on all nodes"},
4881         { "moveip",          control_moveip,            false,  false, "move/failover an ip address to another node", "<ip> <node>"},
4882         { "addip",           control_addip,             true,   false, "add a ip address to a node", "<ip/mask> <iface>"},
4883         { "delip",           control_delip,             false,  false, "delete an ip address from a node", "<ip>"},
4884         { "eventscript",     control_eventscript,       true,   false, "run the eventscript with the given parameters on a node", "<arguments>"},
4885         { "backupdb",        control_backupdb,          false,  false, "backup the database into a file.", "<database> <file>"},
4886         { "restoredb",        control_restoredb,        false,  false, "restore the database from a file.", "<file> [dbname]"},
4887         { "dumpdbbackup",    control_dumpdbbackup,      false,  true,  "dump database backup from a file.", "<file>"},
4888         { "wipedb",           control_wipedb,        false,     false, "wipe the contents of a database.", "<dbname>"},
4889         { "recmaster",        control_recmaster,        false,  false, "show the pnn for the recovery master."},
4890         { "scriptstatus",    control_scriptstatus,  false,      false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
4891         { "enablescript",     control_enablescript,  false,     false, "enable an eventscript", "<script>"},
4892         { "disablescript",    control_disablescript,  false,    false, "disable an eventscript", "<script>"},
4893         { "natgwlist",        control_natgwlist,        false,  false, "show the nodes belonging to this natgw configuration"},
4894         { "xpnn",             control_xpnn,             true,   true,  "find the pnn of the local node without talking to the daemon (unreliable)" },
4895         { "getreclock",       control_getreclock,       false,  false, "Show the reclock file of a node"},
4896         { "setreclock",       control_setreclock,       false,  false, "Set/clear the reclock file of a node", "[filename]"},
4897         { "setnatgwstate",    control_setnatgwstate,    false,  false, "Set NATGW state to on/off", "{on|off}"},
4898         { "setlmasterrole",   control_setlmasterrole,   false,  false, "Set LMASTER role to on/off", "{on|off}"},
4899         { "setrecmasterrole", control_setrecmasterrole, false,  false, "Set RECMASTER role to on/off", "{on|off}"},
4900         { "setdbprio",        control_setdbprio,        false,  false, "Set DB priority", "<dbid> <prio:1-3>"},
4901         { "getdbprio",        control_getdbprio,        false,  false, "Get DB priority", "<dbid>"},
4902         { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
4903         { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
4904         { "sync",            control_ipreallocate,      false,  false,  "wait until ctdbd has synced all state changes" },
4905         { "pfetch",          control_pfetch,            false,  false,  "fetch a record from a persistent database", "<db> <key> [<file>]" },
4906         { "pstore",          control_pstore,            false,  false,  "write a record to a persistent database", "<db> <key> <file containing record>" },
4907         { "tfetch",          control_tfetch,            false,  true,  "fetch a record from a [c]tdb-file", "<tdb-file> <key> [<file>]" },
4908         { "readkey",         control_readkey,           true,   false,  "read the content off a database key", "<tdb-file> <key>" },
4909         { "writekey",        control_writekey,          true,   false,  "write to a database key", "<tdb-file> <key> <value>" },
4910 };
4911
4912 /*
4913   show usage message
4914  */
4915 static void usage(void)
4916 {
4917         int i;
4918         printf(
4919 "Usage: ctdb [options] <control>\n" \
4920 "Options:\n" \
4921 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
4922 "   -Y                 generate machinereadable output\n"
4923 "   -v                 generate verbose output\n"
4924 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
4925         printf("Controls:\n");
4926         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
4927                 printf("  %-15s %-27s  %s\n", 
4928                        ctdb_commands[i].name, 
4929                        ctdb_commands[i].args?ctdb_commands[i].args:"",
4930                        ctdb_commands[i].msg);
4931         }
4932         exit(1);
4933 }
4934
4935
4936 static void ctdb_alarm(int sig)
4937 {
4938         printf("Maximum runtime exceeded - exiting\n");
4939         _exit(ERR_TIMEOUT);
4940 }
4941
4942 /*
4943   main program
4944 */
4945 int main(int argc, const char *argv[])
4946 {
4947         struct ctdb_context *ctdb;
4948         char *nodestring = NULL;
4949         struct poptOption popt_options[] = {
4950                 POPT_AUTOHELP
4951                 POPT_CTDB_CMDLINE
4952                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
4953                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
4954                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
4955                 { "verbose",    'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
4956                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
4957                 POPT_TABLEEND
4958         };
4959         int opt;
4960         const char **extra_argv;
4961         int extra_argc = 0;
4962         int ret=-1, i;
4963         poptContext pc;
4964         struct event_context *ev;
4965         const char *control;
4966
4967         setlinebuf(stdout);
4968         
4969         /* set some defaults */
4970         options.maxruntime = 0;
4971         options.timelimit = 3;
4972         options.pnn = CTDB_CURRENT_NODE;
4973
4974         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
4975
4976         while ((opt = poptGetNextOpt(pc)) != -1) {
4977                 switch (opt) {
4978                 default:
4979                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
4980                                 poptBadOption(pc, 0), poptStrerror(opt)));
4981                         exit(1);
4982                 }
4983         }
4984
4985         /* setup the remaining options for the main program to use */
4986         extra_argv = poptGetArgs(pc);
4987         if (extra_argv) {
4988                 extra_argv++;
4989                 while (extra_argv[extra_argc]) extra_argc++;
4990         }
4991
4992         if (extra_argc < 1) {
4993                 usage();
4994         }
4995
4996         if (options.maxruntime == 0) {
4997                 const char *ctdb_timeout;
4998                 ctdb_timeout = getenv("CTDB_TIMEOUT");
4999                 if (ctdb_timeout != NULL) {
5000                         options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
5001                 } else {
5002                         /* default timeout is 120 seconds */
5003                         options.maxruntime = 120;
5004                 }
5005         }
5006
5007         signal(SIGALRM, ctdb_alarm);
5008         alarm(options.maxruntime);
5009
5010         /* setup the node number to contact */
5011         if (nodestring != NULL) {
5012                 if (strcmp(nodestring, "all") == 0) {
5013                         options.pnn = CTDB_BROADCAST_ALL;
5014                 } else {
5015                         options.pnn = strtoul(nodestring, NULL, 0);
5016                 }
5017         }
5018
5019         control = extra_argv[0];
5020
5021         ev = event_context_init(NULL);
5022         if (!ev) {
5023                 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
5024                 exit(1);
5025         }
5026         tevent_loop_allow_nesting(ev);
5027
5028         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
5029                 if (strcmp(control, ctdb_commands[i].name) == 0) {
5030                         int j;
5031
5032                         if (ctdb_commands[i].without_daemon == true) {
5033                                 close(2);
5034                         }
5035
5036                         if (ctdb_commands[i].without_daemon == false) {
5037                                 const char *socket_name;
5038
5039                                 /* initialise ctdb */
5040                                 ctdb = ctdb_cmdline_client(ev);
5041
5042                                 if (ctdb == NULL) {
5043                                         DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
5044                                         exit(1);
5045                                 }
5046
5047                                 /* initialize a libctdb connection as well */
5048                                 socket_name = ctdb_get_socketname(ctdb);
5049                                 ctdb_connection = ctdb_connect(socket_name,
5050                                                        ctdb_log_file, stderr);
5051                                 if (ctdb_connection == NULL) {
5052                                         fprintf(stderr, "Failed to connect to daemon from libctdb\n");
5053                                         exit(1);
5054                                 }                               
5055                         
5056                                 /* verify the node exists */
5057                                 verify_node(ctdb);
5058
5059                                 if (options.pnn == CTDB_CURRENT_NODE) {
5060                                         int pnn;
5061                                         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
5062                                         if (pnn == -1) {
5063                                                 return -1;
5064                                         }
5065                                         options.pnn = pnn;
5066                                 }
5067                         }
5068
5069                         if (ctdb_commands[i].auto_all && 
5070                             options.pnn == CTDB_BROADCAST_ALL) {
5071                                 uint32_t *nodes;
5072                                 uint32_t num_nodes;
5073                                 ret = 0;
5074
5075                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
5076                                 CTDB_NO_MEMORY(ctdb, nodes);
5077         
5078                                 for (j=0;j<num_nodes;j++) {
5079                                         options.pnn = nodes[j];
5080                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5081                                 }
5082                                 talloc_free(nodes);
5083                         } else {
5084                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
5085                         }
5086                         break;
5087                 }
5088         }
5089
5090         if (i == ARRAY_SIZE(ctdb_commands)) {
5091                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
5092                 exit(1);
5093         }
5094
5095         return ret;
5096 }