Fix treatment of link local ipv6 addresses: set the scope id.
[samba.git] / ctdb / tools / ctdb.c
1 /* 
2    ctdb control tool
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Ronnie Sahlberg  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.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_private.h"
31 #include "../common/rb_tree.h"
32 #include "db_wrap.h"
33
34
35 #define ERR_TIMEOUT     20      /* timed out trying to reach node */
36 #define ERR_NONODE      21      /* node does not exist */
37 #define ERR_DISNODE     22      /* node is disconnected */
38
39 static void usage(void);
40
41 static struct {
42         int timelimit;
43         uint32_t pnn;
44         int machinereadable;
45         int maxruntime;
46 } options;
47
48 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
49
50 #ifdef CTDB_VERS
51 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
52 {
53 #define STR(x) #x
54 #define XSTR(x) STR(x)
55         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
56         return 0;
57 }
58 #endif
59
60
61 /*
62   verify that a node exists and is reachable
63  */
64 static void verify_node(struct ctdb_context *ctdb)
65 {
66         int ret;
67         struct ctdb_node_map *nodemap=NULL;
68
69         if (options.pnn == CTDB_CURRENT_NODE) {
70                 return;
71         }
72         if (options.pnn == CTDB_BROADCAST_ALL) {
73                 return;
74         }
75
76         /* verify the node exists */
77         if (ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap) != 0) {
78                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
79                 exit(10);
80         }
81         if (options.pnn >= nodemap->num) {
82                 DEBUG(DEBUG_ERR, ("Node %u does not exist\n", options.pnn));
83                 exit(ERR_NONODE);
84         }
85         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_DISCONNECTED) {
86                 DEBUG(DEBUG_ERR, ("Node %u is DISCONNECTED\n", options.pnn));
87                 exit(ERR_DISNODE);
88         }
89
90         /* verify we can access the node */
91         ret = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
92         if (ret == -1) {
93                 DEBUG(DEBUG_ERR,("Can not ban node. Node is not operational.\n"));
94                 exit(10);
95         }
96 }
97
98 /*
99  check if a database exists
100 */
101 static int db_exists(struct ctdb_context *ctdb, const char *db_name)
102 {
103         int i, ret;
104         struct ctdb_dbid_map *dbmap=NULL;
105
106         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
107         if (ret != 0) {
108                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
109                 return -1;
110         }
111
112         for(i=0;i<dbmap->num;i++){
113                 const char *name;
114
115                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
116                 if (!strcmp(name, db_name)) {
117                         return 0;
118                 }
119         }
120
121         return -1;
122 }
123
124 /*
125   see if a process exists
126  */
127 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
128 {
129         uint32_t pnn, pid;
130         int ret;
131         if (argc < 1) {
132                 usage();
133         }
134
135         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
136                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
137                 return -1;
138         }
139
140         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
141         if (ret == 0) {
142                 printf("%u:%u exists\n", pnn, pid);
143         } else {
144                 printf("%u:%u does not exist\n", pnn, pid);
145         }
146         return ret;
147 }
148
149 /*
150   display statistics structure
151  */
152 static void show_statistics(struct ctdb_statistics *s)
153 {
154         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
155         int i;
156         const char *prefix=NULL;
157         int preflen=0;
158         const struct {
159                 const char *name;
160                 uint32_t offset;
161         } fields[] = {
162 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
163                 STATISTICS_FIELD(num_clients),
164                 STATISTICS_FIELD(frozen),
165                 STATISTICS_FIELD(recovering),
166                 STATISTICS_FIELD(client_packets_sent),
167                 STATISTICS_FIELD(client_packets_recv),
168                 STATISTICS_FIELD(node_packets_sent),
169                 STATISTICS_FIELD(node_packets_recv),
170                 STATISTICS_FIELD(keepalive_packets_sent),
171                 STATISTICS_FIELD(keepalive_packets_recv),
172                 STATISTICS_FIELD(node.req_call),
173                 STATISTICS_FIELD(node.reply_call),
174                 STATISTICS_FIELD(node.req_dmaster),
175                 STATISTICS_FIELD(node.reply_dmaster),
176                 STATISTICS_FIELD(node.reply_error),
177                 STATISTICS_FIELD(node.req_message),
178                 STATISTICS_FIELD(node.req_control),
179                 STATISTICS_FIELD(node.reply_control),
180                 STATISTICS_FIELD(client.req_call),
181                 STATISTICS_FIELD(client.req_message),
182                 STATISTICS_FIELD(client.req_control),
183                 STATISTICS_FIELD(timeouts.call),
184                 STATISTICS_FIELD(timeouts.control),
185                 STATISTICS_FIELD(timeouts.traverse),
186                 STATISTICS_FIELD(total_calls),
187                 STATISTICS_FIELD(pending_calls),
188                 STATISTICS_FIELD(lockwait_calls),
189                 STATISTICS_FIELD(pending_lockwait_calls),
190                 STATISTICS_FIELD(childwrite_calls),
191                 STATISTICS_FIELD(pending_childwrite_calls),
192                 STATISTICS_FIELD(memory_used),
193                 STATISTICS_FIELD(max_hop_count),
194         };
195         printf("CTDB version %u\n", CTDB_VERSION);
196         for (i=0;i<ARRAY_SIZE(fields);i++) {
197                 if (strchr(fields[i].name, '.')) {
198                         preflen = strcspn(fields[i].name, ".")+1;
199                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
200                                 prefix = fields[i].name;
201                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
202                         }
203                 } else {
204                         preflen = 0;
205                 }
206                 printf(" %*s%-22s%*s%10u\n", 
207                        preflen?4:0, "",
208                        fields[i].name+preflen, 
209                        preflen?0:4, "",
210                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
211         }
212         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
213         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
214         printf(" %-30s     %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
215         talloc_free(tmp_ctx);
216 }
217
218 /*
219   display remote ctdb statistics combined from all nodes
220  */
221 static int control_statistics_all(struct ctdb_context *ctdb)
222 {
223         int ret, i;
224         struct ctdb_statistics statistics;
225         uint32_t *nodes;
226         uint32_t num_nodes;
227
228         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
229         CTDB_NO_MEMORY(ctdb, nodes);
230         
231         ZERO_STRUCT(statistics);
232
233         for (i=0;i<num_nodes;i++) {
234                 struct ctdb_statistics s1;
235                 int j;
236                 uint32_t *v1 = (uint32_t *)&s1;
237                 uint32_t *v2 = (uint32_t *)&statistics;
238                 uint32_t num_ints = 
239                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
240                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
241                 if (ret != 0) {
242                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
243                         return ret;
244                 }
245                 for (j=0;j<num_ints;j++) {
246                         v2[j] += v1[j];
247                 }
248                 statistics.max_hop_count = 
249                         MAX(statistics.max_hop_count, s1.max_hop_count);
250                 statistics.max_call_latency = 
251                         MAX(statistics.max_call_latency, s1.max_call_latency);
252                 statistics.max_lockwait_latency = 
253                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
254         }
255         talloc_free(nodes);
256         printf("Gathered statistics for %u nodes\n", num_nodes);
257         show_statistics(&statistics);
258         return 0;
259 }
260
261 /*
262   display remote ctdb statistics
263  */
264 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
265 {
266         int ret;
267         struct ctdb_statistics statistics;
268
269         if (options.pnn == CTDB_BROADCAST_ALL) {
270                 return control_statistics_all(ctdb);
271         }
272
273         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
274         if (ret != 0) {
275                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
276                 return ret;
277         }
278         show_statistics(&statistics);
279         return 0;
280 }
281
282
283 /*
284   reset remote ctdb statistics
285  */
286 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
287 {
288         int ret;
289
290         ret = ctdb_statistics_reset(ctdb, options.pnn);
291         if (ret != 0) {
292                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
293                 return ret;
294         }
295         return 0;
296 }
297
298
299 /*
300   display uptime of remote node
301  */
302 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
303 {
304         int ret;
305         struct ctdb_uptime *uptime = NULL;
306         int tmp, days, hours, minutes, seconds;
307
308         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
309         if (ret != 0) {
310                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
311                 return ret;
312         }
313
314         if (options.machinereadable){
315                 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:Last Recovery Duration:\n");
316                 printf(":%u:%u:%u:%lf\n",
317                         (unsigned int)uptime->current_time.tv_sec,
318                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
319                         (unsigned int)uptime->last_recovery_finished.tv_sec,
320                         timeval_delta(&uptime->last_recovery_finished,
321                                       &uptime->last_recovery_started)
322                 );
323                 return 0;
324         }
325
326         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
327
328         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
329         seconds = tmp%60;
330         tmp    /= 60;
331         minutes = tmp%60;
332         tmp    /= 60;
333         hours   = tmp%24;
334         tmp    /= 24;
335         days    = tmp;
336         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
337
338         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
339         seconds = tmp%60;
340         tmp    /= 60;
341         minutes = tmp%60;
342         tmp    /= 60;
343         hours   = tmp%24;
344         tmp    /= 24;
345         days    = tmp;
346         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
347         
348         printf("Duration of last recovery : %lf seconds\n",
349                 timeval_delta(&uptime->last_recovery_finished,
350                               &uptime->last_recovery_started));
351
352         return 0;
353 }
354
355 /*
356   show the PNN of the current node
357  */
358 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
359 {
360         int mypnn;
361
362         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
363         if (mypnn == -1) {
364                 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node."));
365                 return -1;
366         }
367
368         printf("PNN:%d\n", mypnn);
369         return 0;
370 }
371
372 /*
373   display remote ctdb status
374  */
375 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
376 {
377         int i, ret;
378         struct ctdb_vnn_map *vnnmap=NULL;
379         struct ctdb_node_map *nodemap=NULL;
380         uint32_t recmode, recmaster;
381         int mypnn;
382
383         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
384         if (mypnn == -1) {
385                 return -1;
386         }
387
388         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
389         if (ret != 0) {
390                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
391                 return ret;
392         }
393
394         if(options.machinereadable){
395                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
396                 for(i=0;i<nodemap->num;i++){
397                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
398                                 ctdb_addr_to_str(&nodemap->nodes[i].addr),
399                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
400                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
401                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
402                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
403                 }
404                 return 0;
405         }
406
407         printf("Number of nodes:%d\n", nodemap->num);
408         for(i=0;i<nodemap->num;i++){
409                 static const struct {
410                         uint32_t flag;
411                         const char *name;
412                 } flag_names[] = {
413                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
414                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
415                         { NODE_FLAGS_BANNED,                "BANNED" },
416                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
417                 };
418                 char *flags_str = NULL;
419                 int j;
420                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
421                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
422                                 if (flags_str == NULL) {
423                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
424                                 } else {
425                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
426                                                                            flag_names[j].name);
427                                 }
428                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
429                         }
430                 }
431                 if (flags_str == NULL) {
432                         flags_str = talloc_strdup(ctdb, "OK");
433                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
434                 }
435                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
436                        ctdb_addr_to_str(&nodemap->nodes[i].addr),
437                        flags_str,
438                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
439                 talloc_free(flags_str);
440         }
441
442         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
443         if (ret != 0) {
444                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
445                 return ret;
446         }
447         if (vnnmap->generation == INVALID_GENERATION) {
448                 printf("Generation:INVALID\n");
449         } else {
450                 printf("Generation:%d\n",vnnmap->generation);
451         }
452         printf("Size:%d\n",vnnmap->size);
453         for(i=0;i<vnnmap->size;i++){
454                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
455         }
456
457         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
458         if (ret != 0) {
459                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
460                 return ret;
461         }
462         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
463
464         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
465         if (ret != 0) {
466                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
467                 return ret;
468         }
469         printf("Recovery master:%d\n",recmaster);
470
471         return 0;
472 }
473
474 /*
475   display the pnn of the recovery master
476  */
477 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
478 {
479         int ret;
480         uint32_t recmaster;
481
482         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
483         if (ret != 0) {
484                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
485                 return ret;
486         }
487         printf("%d\n",recmaster);
488
489         return 0;
490 }
491
492 /*
493   get a list of all tickles for this pnn
494  */
495 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
496 {
497         struct ctdb_control_tcp_tickle_list *list;
498         ctdb_sock_addr addr;
499         int i, ret;
500
501         if (argc < 1) {
502                 usage();
503         }
504
505         if (parse_ip(argv[0], NULL, &addr) == 0) {
506                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
507                 return -1;
508         }
509
510         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
511         if (ret == -1) {
512                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
513                 return -1;
514         }
515
516         printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
517         printf("Num tickles:%u\n", list->tickles.num);
518         for (i=0;i<list->tickles.num;i++) {
519                 printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
520                 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));
521         }
522
523         talloc_free(list);
524         
525         return 0;
526 }
527
528 /* send a release ip to all nodes */
529 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
530 ctdb_sock_addr *addr)
531 {
532         int ret;
533         struct ctdb_public_ip pip;
534         TDB_DATA data;
535         struct ctdb_node_map *nodemap=NULL;
536
537         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
538         if (ret != 0) {
539                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
540                 return ret;
541         }
542
543         /* send a moveip message to the recovery master */
544         pip.pnn    = pnn;
545         pip.addr   = *addr;
546         data.dsize = sizeof(pip);
547         data.dptr  = (unsigned char *)&pip;
548
549
550         /* send release ip to all nodes */
551         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
552                         list_of_active_nodes(ctdb, nodemap, ctdb, true),
553                         TIMELIMIT(), false, data,
554                         NULL, NULL, NULL) != 0) {
555                 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
556                 return -1;
557         }
558
559         return 0;
560 }
561
562 /*
563   move/failover an ip address to a specific node
564  */
565 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
566 {
567         uint32_t pnn;
568         ctdb_sock_addr addr;
569         uint32_t value;
570         struct ctdb_all_public_ips *ips;
571         int i, ret;
572
573         if (argc < 2) {
574                 usage();
575         }
576
577         if (parse_ip(argv[0], NULL,  &addr) == 0) {
578                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
579                 return -1;
580         }
581
582
583         if (sscanf(argv[1], "%u", &pnn) != 1) {
584                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
585                 return -1;
586         }
587
588         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
589         if (ret == -1) {
590                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
591                 return -1;
592         }
593         if (value != 0) {
594                 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
595                 return -1;
596         }
597
598         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
599         if (ret == -1) {
600                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
601                 return -1;
602         }
603         if (value == 0) {
604                 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
605                 return -1;
606         }
607
608         /* read the public ip list from the node */
609         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
610         if (ret != 0) {
611                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
612                 return -1;
613         }
614
615         for (i=0;i<ips->num;i++) {
616                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
617                         break;
618                 }
619         }
620         if (i==ips->num) {
621                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
622                         pnn, ctdb_addr_to_str(&addr)));
623                 return -1;
624         }
625         if (ips->ips[i].pnn == pnn) {
626                 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
627                         pnn, ctdb_addr_to_str(&ips->ips[i].addr)));
628                 return -1;
629         }
630
631         ret = control_send_release(ctdb, pnn, &ips->ips[i].addr);
632         if (ret != 0) {
633                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
634                 return -1;
635         }
636
637         return 0;
638 }
639
640 void getips_store_callback(void *param, void *data)
641 {
642         struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
643         struct ctdb_all_public_ips *ips = param;
644         int i;
645
646         i = ips->num++;
647         ips->ips[i].pnn  = node_ip->pnn;
648         ips->ips[i].addr = node_ip->addr;
649 }
650
651 void getips_count_callback(void *param, void *data)
652 {
653         uint32_t *count = param;
654
655         (*count)++;
656 }
657
658 #define IP_KEYLEN       4
659 static uint32_t *ip_key(ctdb_sock_addr *ip)
660 {
661         static uint32_t key[IP_KEYLEN];
662
663         bzero(key, sizeof(key));
664
665         switch (ip->sa.sa_family) {
666         case AF_INET:
667                 key[0]  = ip->ip.sin_addr.s_addr;
668                 break;
669         case AF_INET6:
670                 key[0]  = ip->ip6.sin6_addr.s6_addr32[3];
671                 key[1]  = ip->ip6.sin6_addr.s6_addr32[2];
672                 key[2]  = ip->ip6.sin6_addr.s6_addr32[1];
673                 key[3]  = ip->ip6.sin6_addr.s6_addr32[0];
674                 break;
675         default:
676                 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
677                 return key;
678         }
679
680         return key;
681 }
682
683 static void *add_ip_callback(void *parm, void *data)
684 {
685         return parm;
686 }
687
688 static int
689 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
690 {
691         struct ctdb_all_public_ips *tmp_ips;
692         struct ctdb_node_map *nodemap=NULL;
693         trbt_tree_t *ip_tree;
694         int i, j, len, ret;
695         uint32_t count;
696
697         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
698         if (ret != 0) {
699                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
700                 return ret;
701         }
702
703         ip_tree = trbt_create(tmp_ctx, 0);
704
705         for(i=0;i<nodemap->num;i++){
706                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
707                         continue;
708                 }
709
710                 /* read the public ip list from this node */
711                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
712                 if (ret != 0) {
713                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
714                         return -1;
715                 }
716         
717                 for (j=0; j<tmp_ips->num;j++) {
718                         struct ctdb_public_ip *node_ip;
719
720                         node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
721                         node_ip->pnn  = tmp_ips->ips[j].pnn;
722                         node_ip->addr = tmp_ips->ips[j].addr;
723
724                         trbt_insertarray32_callback(ip_tree,
725                                 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
726                                 add_ip_callback,
727                                 node_ip);
728                 }
729                 talloc_free(tmp_ips);
730         }
731
732         /* traverse */
733         count = 0;
734         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
735
736         len = offsetof(struct ctdb_all_public_ips, ips) + 
737                 count*sizeof(struct ctdb_public_ip);
738         tmp_ips = talloc_zero_size(tmp_ctx, len);
739         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
740
741         *ips = tmp_ips;
742
743         return 0;
744 }
745
746
747 /* 
748  * scans all other nodes and returns a pnn for another node that can host this 
749  * ip address or -1
750  */
751 static int
752 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
753 {
754         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
755         struct ctdb_all_public_ips *ips;
756         struct ctdb_node_map *nodemap=NULL;
757         int i, j, ret;
758
759         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
760         if (ret != 0) {
761                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
762                 talloc_free(tmp_ctx);
763                 return ret;
764         }
765
766         for(i=0;i<nodemap->num;i++){
767                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
768                         continue;
769                 }
770                 if (nodemap->nodes[i].pnn == options.pnn) {
771                         continue;
772                 }
773
774                 /* read the public ip list from this node */
775                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
776                 if (ret != 0) {
777                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
778                         return -1;
779                 }
780
781                 for (j=0;j<ips->num;j++) {
782                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
783                                 talloc_free(tmp_ctx);
784                                 return nodemap->nodes[i].pnn;
785                         }
786                 }
787                 talloc_free(ips);
788         }
789
790         talloc_free(tmp_ctx);
791         return -1;
792 }
793
794 /*
795   add a public ip address to a node
796  */
797 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
798 {
799         int i, ret;
800         int len;
801         unsigned mask;
802         ctdb_sock_addr addr;
803         struct ctdb_control_ip_iface *pub;
804         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
805         struct ctdb_all_public_ips *ips;
806
807         if (argc != 2) {
808                 talloc_free(tmp_ctx);
809                 usage();
810         }
811
812         if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
813                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
814                 talloc_free(tmp_ctx);
815                 return -1;
816         }
817
818         ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
819         if (ret != 0) {
820                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
821                 talloc_free(tmp_ctx);
822                 return ret;
823         }
824
825
826         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
827         pub = talloc_size(tmp_ctx, len); 
828         CTDB_NO_MEMORY(ctdb, pub);
829
830         pub->addr  = addr;
831         pub->mask  = mask;
832         pub->len   = strlen(argv[1])+1;
833         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
834
835         ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
836         if (ret != 0) {
837                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
838                 talloc_free(tmp_ctx);
839                 return ret;
840         }
841
842
843         /* check if some other node is already serving this ip, if not,
844          * we will claim it
845          */
846         for (i=0;i<ips->num;i++) {
847                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
848                         break;
849                 }
850         }
851         /* no one has this ip so we claim it */
852         if (i == ips->num) {
853                 ret = control_send_release(ctdb, options.pnn, &addr);
854         } else {
855                 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
856         }
857
858         if (ret != 0) {
859                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
860                 return -1;
861         }
862
863         talloc_free(tmp_ctx);
864         return 0;
865 }
866
867 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
868
869 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
870 {
871         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
872         struct ctdb_node_map *nodemap=NULL;
873         struct ctdb_all_public_ips *ips;
874         int ret, i, j;
875
876         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
877         if (ret != 0) {
878                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
879                 return ret;
880         }
881
882         /* remove it from the nodes that are not hosting the ip currently */
883         for(i=0;i<nodemap->num;i++){
884                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
885                         continue;
886                 }
887                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
888                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
889                         continue;
890                 }
891
892                 for (j=0;j<ips->num;j++) {
893                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
894                                 break;
895                         }
896                 }
897                 if (j==ips->num) {
898                         continue;
899                 }
900
901                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
902                         continue;
903                 }
904
905                 options.pnn = nodemap->nodes[i].pnn;
906                 control_delip(ctdb, argc, argv);
907         }
908
909
910         /* remove it from every node (also the one hosting it) */
911         for(i=0;i<nodemap->num;i++){
912                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
913                         continue;
914                 }
915                 if (ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips) != 0) {
916                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
917                         continue;
918                 }
919
920                 for (j=0;j<ips->num;j++) {
921                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
922                                 break;
923                         }
924                 }
925                 if (j==ips->num) {
926                         continue;
927                 }
928
929                 options.pnn = nodemap->nodes[i].pnn;
930                 control_delip(ctdb, argc, argv);
931         }
932
933         talloc_free(tmp_ctx);
934         return 0;
935 }
936         
937 /*
938   delete a public ip address from a node
939  */
940 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
941 {
942         int i, ret;
943         ctdb_sock_addr addr;
944         struct ctdb_control_ip_iface pub;
945         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
946         struct ctdb_all_public_ips *ips;
947
948         if (argc != 1) {
949                 talloc_free(tmp_ctx);
950                 usage();
951         }
952
953         if (parse_ip(argv[0], NULL, &addr) == 0) {
954                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
955                 return -1;
956         }
957
958         if (options.pnn == CTDB_BROADCAST_ALL) {
959                 return control_delip_all(ctdb, argc, argv, &addr);
960         }
961
962         pub.addr  = addr;
963         pub.mask  = 0;
964         pub.len   = 0;
965
966         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
967         if (ret != 0) {
968                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
969                 talloc_free(tmp_ctx);
970                 return ret;
971         }
972         
973         for (i=0;i<ips->num;i++) {
974                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
975                         break;
976                 }
977         }
978
979         if (i==ips->num) {
980                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
981                         ctdb_addr_to_str(&addr)));
982                 talloc_free(tmp_ctx);
983                 return -1;
984         }
985
986         if (ips->ips[i].pnn == options.pnn) {
987                 ret = find_other_host_for_public_ip(ctdb, &addr);
988                 if (ret != -1) {
989                         ret = control_send_release(ctdb, ret, &addr);
990                         if (ret != 0) {
991                                 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
992                         }
993                 }
994         }
995
996         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
997         if (ret != 0) {
998                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
999                 talloc_free(tmp_ctx);
1000                 return ret;
1001         }
1002
1003         talloc_free(tmp_ctx);
1004         return 0;
1005 }
1006
1007 /*
1008   kill a tcp connection
1009  */
1010 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1011 {
1012         int ret;
1013         struct ctdb_control_killtcp killtcp;
1014
1015         if (argc < 2) {
1016                 usage();
1017         }
1018
1019         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
1020                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1021                 return -1;
1022         }
1023
1024         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
1025                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1026                 return -1;
1027         }
1028
1029         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
1030         if (ret != 0) {
1031                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
1032                 return ret;
1033         }
1034
1035         return 0;
1036 }
1037
1038
1039 /*
1040   send a gratious arp
1041  */
1042 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
1043 {
1044         int ret;
1045         ctdb_sock_addr addr;
1046
1047         if (argc < 2) {
1048                 usage();
1049         }
1050
1051         if (!parse_ip(argv[0], NULL, &addr)) {
1052                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
1053                 return -1;
1054         }
1055
1056         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
1057         if (ret != 0) {
1058                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
1059                 return ret;
1060         }
1061
1062         return 0;
1063 }
1064
1065 /*
1066   register a server id
1067  */
1068 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1069 {
1070         int ret;
1071         struct ctdb_server_id server_id;
1072
1073         if (argc < 3) {
1074                 usage();
1075         }
1076
1077         server_id.pnn       = strtoul(argv[0], NULL, 0);
1078         server_id.type      = strtoul(argv[1], NULL, 0);
1079         server_id.server_id = strtoul(argv[2], NULL, 0);
1080
1081         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
1082         if (ret != 0) {
1083                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
1084                 return ret;
1085         }
1086         return -1;
1087 }
1088
1089 /*
1090   unregister a server id
1091  */
1092 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1093 {
1094         int ret;
1095         struct ctdb_server_id server_id;
1096
1097         if (argc < 3) {
1098                 usage();
1099         }
1100
1101         server_id.pnn       = strtoul(argv[0], NULL, 0);
1102         server_id.type      = strtoul(argv[1], NULL, 0);
1103         server_id.server_id = strtoul(argv[2], NULL, 0);
1104
1105         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
1106         if (ret != 0) {
1107                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
1108                 return ret;
1109         }
1110         return -1;
1111 }
1112
1113 /*
1114   check if a server id exists
1115  */
1116 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
1117 {
1118         uint32_t status;
1119         int ret;
1120         struct ctdb_server_id server_id;
1121
1122         if (argc < 3) {
1123                 usage();
1124         }
1125
1126         server_id.pnn       = strtoul(argv[0], NULL, 0);
1127         server_id.type      = strtoul(argv[1], NULL, 0);
1128         server_id.server_id = strtoul(argv[2], NULL, 0);
1129
1130         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
1131         if (ret != 0) {
1132                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
1133                 return ret;
1134         }
1135
1136         if (status) {
1137                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
1138         } else {
1139                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
1140         }
1141         return 0;
1142 }
1143
1144 /*
1145   get a list of all server ids that are registered on a node
1146  */
1147 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
1148 {
1149         int i, ret;
1150         struct ctdb_server_id_list *server_ids;
1151
1152         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
1153         if (ret != 0) {
1154                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
1155                 return ret;
1156         }
1157
1158         for (i=0; i<server_ids->num; i++) {
1159                 printf("Server id %d:%d:%d\n", 
1160                         server_ids->server_ids[i].pnn, 
1161                         server_ids->server_ids[i].type, 
1162                         server_ids->server_ids[i].server_id); 
1163         }
1164
1165         return -1;
1166 }
1167
1168 /*
1169   send a tcp tickle ack
1170  */
1171 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
1172 {
1173         int ret;
1174         ctdb_sock_addr  src, dst;
1175
1176         if (argc < 2) {
1177                 usage();
1178         }
1179
1180         if (!parse_ip_port(argv[0], &src)) {
1181                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
1182                 return -1;
1183         }
1184
1185         if (!parse_ip_port(argv[1], &dst)) {
1186                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
1187                 return -1;
1188         }
1189
1190         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
1191         if (ret==0) {
1192                 return 0;
1193         }
1194         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
1195
1196         return -1;
1197 }
1198
1199
1200 /*
1201   display public ip status
1202  */
1203 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1204 {
1205         int i, ret;
1206         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1207         struct ctdb_all_public_ips *ips;
1208
1209         if (options.pnn == CTDB_BROADCAST_ALL) {
1210                 /* read the list of public ips from all nodes */
1211                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1212         } else {
1213                 /* read the public ip list from this node */
1214                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1215         }
1216         if (ret != 0) {
1217                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1218                 talloc_free(tmp_ctx);
1219                 return ret;
1220         }
1221
1222         if (options.machinereadable){
1223                 printf(":Public IP:Node:\n");
1224         } else {
1225                 if (options.pnn == CTDB_BROADCAST_ALL) {
1226                         printf("Public IPs on ALL nodes\n");
1227                 } else {
1228                         printf("Public IPs on node %u\n", options.pnn);
1229                 }
1230         }
1231
1232         for (i=1;i<=ips->num;i++) {
1233                 if (options.machinereadable){
1234                         printf(":%s:%d:\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1235                 } else {
1236                         printf("%s %d\n", ctdb_addr_to_str(&ips->ips[ips->num-i].addr), ips->ips[ips->num-i].pnn);
1237                 }
1238         }
1239
1240         talloc_free(tmp_ctx);
1241         return 0;
1242 }
1243
1244 /*
1245   display pid of a ctdb daemon
1246  */
1247 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1248 {
1249         uint32_t pid;
1250         int ret;
1251
1252         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1253         if (ret != 0) {
1254                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1255                 return ret;
1256         }
1257         printf("Pid:%d\n", pid);
1258
1259         return 0;
1260 }
1261
1262 /*
1263   disable a remote node
1264  */
1265 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1266 {
1267         int ret;
1268
1269         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1270         if (ret != 0) {
1271                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1272                 return ret;
1273         }
1274
1275         return 0;
1276 }
1277
1278 /*
1279   enable a disabled remote node
1280  */
1281 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1282 {
1283         int ret;
1284
1285         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1286         if (ret != 0) {
1287                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1288                 return ret;
1289         }
1290
1291         return 0;
1292 }
1293
1294 static uint32_t get_generation(struct ctdb_context *ctdb)
1295 {
1296         struct ctdb_vnn_map *vnnmap=NULL;
1297         int ret;
1298
1299         /* wait until the recmaster is not in recovery mode */
1300         while (1) {
1301                 uint32_t recmode, recmaster;
1302                 
1303                 if (vnnmap != NULL) {
1304                         talloc_free(vnnmap);
1305                         vnnmap = NULL;
1306                 }
1307
1308                 /* get the recmaster */
1309                 ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
1310                 if (ret != 0) {
1311                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1312                         exit(10);
1313                 }
1314
1315                 /* get recovery mode */
1316                 ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), recmaster, &recmode);
1317                 if (ret != 0) {
1318                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1319                         exit(10);
1320                 }
1321
1322                 /* get the current generation number */
1323                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, ctdb, &vnnmap);
1324                 if (ret != 0) {
1325                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
1326                         exit(10);
1327                 }
1328
1329                 if ((recmode == CTDB_RECOVERY_NORMAL)
1330                 &&  (vnnmap->generation != 1)){
1331                         return vnnmap->generation;
1332                 }
1333                 sleep(1);
1334         }
1335 }
1336
1337 /*
1338   ban a node from the cluster
1339  */
1340 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1341 {
1342         int ret;
1343         struct ctdb_ban_info b;
1344         TDB_DATA data;
1345         uint32_t ban_time;
1346         struct ctdb_node_map *nodemap=NULL;
1347         uint32_t generation, next_generation;
1348
1349         if (argc < 1) {
1350                 usage();
1351         }
1352         
1353         /* record the current generation number */
1354         generation = get_generation(ctdb);
1355
1356
1357         /* verify the node exists */
1358         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1359         if (ret != 0) {
1360                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1361                 return ret;
1362         }
1363
1364         if (nodemap->nodes[options.pnn].flags & NODE_FLAGS_BANNED) {
1365                 DEBUG(DEBUG_ERR,("Node %u is already banned.\n", options.pnn));
1366                 return -1;
1367         }
1368
1369         ban_time = strtoul(argv[0], NULL, 0);
1370
1371         b.pnn = options.pnn;
1372         b.ban_time = ban_time;
1373
1374         data.dptr = (uint8_t *)&b;
1375         data.dsize = sizeof(b);
1376
1377         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1378         if (ret != 0) {
1379                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1380                 return -1;
1381         }
1382
1383         /* wait until we are in a new generation */
1384         while (1) {
1385                 next_generation = get_generation(ctdb);
1386                 if (next_generation != generation) {
1387                         return 0;
1388                 }
1389                 sleep(1);
1390         }
1391
1392         return 0;
1393 }
1394
1395
1396 /*
1397   unban a node from the cluster
1398  */
1399 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1400 {
1401         int ret;
1402         TDB_DATA data;
1403         uint32_t generation, next_generation;
1404
1405         /* record the current generation number */
1406         generation = get_generation(ctdb);
1407
1408         data.dptr = (uint8_t *)&options.pnn;
1409         data.dsize = sizeof(uint32_t);
1410
1411         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1412         if (ret != 0) {
1413                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1414                 return -1;
1415         }
1416         
1417         /* wait until we are in a new generation */
1418         while (1) {
1419                 next_generation = get_generation(ctdb);
1420                 if (next_generation != generation) {
1421                         return 0;
1422                 }
1423                 sleep(1);
1424         }
1425
1426         return 0;
1427 }
1428
1429
1430 /*
1431   shutdown a daemon
1432  */
1433 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1434 {
1435         int ret;
1436
1437         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1438         if (ret != 0) {
1439                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1440                 return ret;
1441         }
1442
1443         return 0;
1444 }
1445
1446 /*
1447   trigger a recovery
1448  */
1449 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1450 {
1451         int ret;
1452         uint32_t generation, next_generation;
1453
1454         /* record the current generation number */
1455         generation = get_generation(ctdb);
1456
1457         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1458         if (ret != 0) {
1459                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1460                 return ret;
1461         }
1462
1463         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1464         if (ret != 0) {
1465                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1466                 return ret;
1467         }
1468
1469         /* wait until we are in a new generation */
1470         while (1) {
1471                 next_generation = get_generation(ctdb);
1472                 if (next_generation != generation) {
1473                         return 0;
1474                 }
1475                 sleep(1);
1476         }
1477
1478         return 0;
1479 }
1480
1481
1482 /*
1483   display monitoring mode of a remote node
1484  */
1485 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1486 {
1487         uint32_t monmode;
1488         int ret;
1489
1490         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1491         if (ret != 0) {
1492                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1493                 return ret;
1494         }
1495         if (!options.machinereadable){
1496                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1497         } else {
1498                 printf(":mode:\n");
1499                 printf(":%d:\n",monmode);
1500         }
1501         return 0;
1502 }
1503
1504
1505 /*
1506   display capabilities of a remote node
1507  */
1508 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1509 {
1510         uint32_t capabilities;
1511         int ret;
1512
1513         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1514         if (ret != 0) {
1515                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1516                 return ret;
1517         }
1518         
1519         if (!options.machinereadable){
1520                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1521                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1522                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
1523         } else {
1524                 printf(":RECMASTER:LMASTER:LVS:\n");
1525                 printf(":%d:%d:%d:\n",
1526                         !!(capabilities&CTDB_CAP_RECMASTER),
1527                         !!(capabilities&CTDB_CAP_LMASTER),
1528                         !!(capabilities&CTDB_CAP_LVS));
1529         }
1530         return 0;
1531 }
1532
1533 /*
1534   display lvs configuration
1535  */
1536 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
1537 {
1538         uint32_t *capabilities;
1539         struct ctdb_node_map *nodemap=NULL;
1540         int i, ret;
1541         int healthy_count = 0;
1542
1543         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1544         if (ret != 0) {
1545                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1546                 return ret;
1547         }
1548
1549         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1550         CTDB_NO_MEMORY(ctdb, capabilities);
1551         
1552         /* collect capabilities for all connected nodes */
1553         for (i=0; i<nodemap->num; i++) {
1554                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1555                         continue;
1556                 }
1557                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1558                         continue;
1559                 }
1560         
1561                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1562                 if (ret != 0) {
1563                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1564                         return ret;
1565                 }
1566
1567                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1568                         continue;
1569                 }
1570
1571                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1572                         healthy_count++;
1573                 }
1574         }
1575
1576         /* Print all LVS nodes */
1577         for (i=0; i<nodemap->num; i++) {
1578                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1579                         continue;
1580                 }
1581                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1582                         continue;
1583                 }
1584                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1585                         continue;
1586                 }
1587
1588                 if (healthy_count != 0) {
1589                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1590                                 continue;
1591                         }
1592                 }
1593
1594                 printf("%d:%s\n", i, 
1595                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
1596         }
1597
1598         return 0;
1599 }
1600
1601 /*
1602   display who is the lvs master
1603  */
1604 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1605 {
1606         uint32_t *capabilities;
1607         struct ctdb_node_map *nodemap=NULL;
1608         int i, ret;
1609         int healthy_count = 0;
1610
1611         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1612         if (ret != 0) {
1613                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1614                 return ret;
1615         }
1616
1617         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
1618         CTDB_NO_MEMORY(ctdb, capabilities);
1619         
1620         /* collect capabilities for all connected nodes */
1621         for (i=0; i<nodemap->num; i++) {
1622                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1623                         continue;
1624                 }
1625                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1626                         continue;
1627                 }
1628         
1629                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
1630                 if (ret != 0) {
1631                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
1632                         return ret;
1633                 }
1634
1635                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1636                         continue;
1637                 }
1638
1639                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
1640                         healthy_count++;
1641                 }
1642         }
1643
1644         /* find and show the lvsmaster */
1645         for (i=0; i<nodemap->num; i++) {
1646                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1647                         continue;
1648                 }
1649                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
1650                         continue;
1651                 }
1652                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
1653                         continue;
1654                 }
1655
1656                 if (healthy_count != 0) {
1657                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
1658                                 continue;
1659                         }
1660                 }
1661
1662                 printf("Node %d is LVS master\n", i);
1663                 return 0;
1664         }
1665
1666         printf("There is no LVS master\n");
1667         return 0;
1668 }
1669
1670 /*
1671   disable monitoring on a  node
1672  */
1673 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1674 {
1675         
1676         int ret;
1677
1678         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1679         if (ret != 0) {
1680                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1681                 return ret;
1682         }
1683         printf("Monitoring mode:%s\n","DISABLED");
1684
1685         return 0;
1686 }
1687
1688 /*
1689   enable monitoring on a  node
1690  */
1691 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1692 {
1693         
1694         int ret;
1695
1696         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1697         if (ret != 0) {
1698                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1699                 return ret;
1700         }
1701         printf("Monitoring mode:%s\n","ACTIVE");
1702
1703         return 0;
1704 }
1705
1706 /*
1707   display remote list of keys/data for a db
1708  */
1709 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1710 {
1711         const char *db_name;
1712         struct ctdb_db_context *ctdb_db;
1713         int ret;
1714
1715         if (argc < 1) {
1716                 usage();
1717         }
1718
1719         db_name = argv[0];
1720
1721
1722         if (db_exists(ctdb, db_name)) {
1723                 DEBUG(DEBUG_ERR,("Database '%s' does not exist\n", db_name));
1724                 return -1;
1725         }
1726
1727         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1728
1729         if (ctdb_db == NULL) {
1730                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1731                 return -1;
1732         }
1733
1734         /* traverse and dump the cluster tdb */
1735         ret = ctdb_dump_db(ctdb_db, stdout);
1736         if (ret == -1) {
1737                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1738                 return -1;
1739         }
1740         talloc_free(ctdb_db);
1741
1742         printf("Dumped %d records\n", ret);
1743         return 0;
1744 }
1745
1746
1747 /*
1748   display a list of the databases on a remote ctdb
1749  */
1750 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1751 {
1752         int i, ret;
1753         struct ctdb_dbid_map *dbmap=NULL;
1754
1755         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1756         if (ret != 0) {
1757                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1758                 return ret;
1759         }
1760
1761         printf("Number of databases:%d\n", dbmap->num);
1762         for(i=0;i<dbmap->num;i++){
1763                 const char *path;
1764                 const char *name;
1765                 bool persistent;
1766
1767                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1768                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1769                 persistent = dbmap->dbs[i].persistent;
1770                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
1771                        path, persistent?"PERSISTENT":"");
1772         }
1773
1774         return 0;
1775 }
1776
1777 /*
1778   check if the local node is recmaster or not
1779   it will return 1 if this node is the recmaster and 0 if it is not
1780   or if the local ctdb daemon could not be contacted
1781  */
1782 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1783 {
1784         uint32_t mypnn, recmaster;
1785         int ret;
1786
1787         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1788         if (mypnn == -1) {
1789                 printf("Failed to get pnn of node\n");
1790                 return 1;
1791         }
1792
1793         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1794         if (ret != 0) {
1795                 printf("Failed to get the recmaster\n");
1796                 return 1;
1797         }
1798
1799         if (recmaster != mypnn) {
1800                 printf("this node is not the recmaster\n");
1801                 return 1;
1802         }
1803
1804         printf("this node is the recmaster\n");
1805         return 0;
1806 }
1807
1808 /*
1809   ping a node
1810  */
1811 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1812 {
1813         int ret;
1814         struct timeval tv = timeval_current();
1815         ret = ctdb_ctrl_ping(ctdb, options.pnn);
1816         if (ret == -1) {
1817                 printf("Unable to get ping response from node %u\n", options.pnn);
1818                 return -1;
1819         } else {
1820                 printf("response from %u time=%.6f sec  (%d clients)\n", 
1821                        options.pnn, timeval_elapsed(&tv), ret);
1822         }
1823         return 0;
1824 }
1825
1826
1827 /*
1828   get a tunable
1829  */
1830 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1831 {
1832         const char *name;
1833         uint32_t value;
1834         int ret;
1835
1836         if (argc < 1) {
1837                 usage();
1838         }
1839
1840         name = argv[0];
1841         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1842         if (ret == -1) {
1843                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1844                 return -1;
1845         }
1846
1847         printf("%-19s = %u\n", name, value);
1848         return 0;
1849 }
1850
1851 /*
1852   set a tunable
1853  */
1854 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1855 {
1856         const char *name;
1857         uint32_t value;
1858         int ret;
1859
1860         if (argc < 2) {
1861                 usage();
1862         }
1863
1864         name = argv[0];
1865         value = strtoul(argv[1], NULL, 0);
1866
1867         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1868         if (ret == -1) {
1869                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1870                 return -1;
1871         }
1872         return 0;
1873 }
1874
1875 /*
1876   list all tunables
1877  */
1878 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1879 {
1880         uint32_t count;
1881         const char **list;
1882         int ret, i;
1883
1884         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1885         if (ret == -1) {
1886                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1887                 return -1;
1888         }
1889
1890         for (i=0;i<count;i++) {
1891                 control_getvar(ctdb, 1, &list[i]);
1892         }
1893
1894         talloc_free(list);
1895         
1896         return 0;
1897 }
1898
1899 static struct {
1900         int32_t level;
1901         const char *description;
1902 } debug_levels[] = {
1903         {DEBUG_EMERG,   "EMERG"},
1904         {DEBUG_ALERT,   "ALERT"},
1905         {DEBUG_CRIT,    "CRIT"},
1906         {DEBUG_ERR,     "ERR"},
1907         {DEBUG_WARNING, "WARNING"},
1908         {DEBUG_NOTICE,  "NOTICE"},
1909         {DEBUG_INFO,    "INFO"},
1910         {DEBUG_DEBUG,   "DEBUG"}
1911 };
1912
1913 static const char *get_debug_by_level(int32_t level)
1914 {
1915         int i;
1916
1917         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1918                 if (debug_levels[i].level == level) {
1919                         return debug_levels[i].description;
1920                 }
1921         }
1922         return "Unknown";
1923 }
1924
1925 static int32_t get_debug_by_desc(const char *desc)
1926 {
1927         int i;
1928
1929         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1930                 if (!strcmp(debug_levels[i].description, desc)) {
1931                         return debug_levels[i].level;
1932                 }
1933         }
1934
1935         fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
1936         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1937                 fprintf(stderr, "    %s\n", debug_levels[i].description);
1938         }
1939
1940         exit(10);
1941 }
1942
1943 /*
1944   display debug level on a node
1945  */
1946 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1947 {
1948         int ret;
1949         int32_t level;
1950
1951         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1952         if (ret != 0) {
1953                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1954                 return ret;
1955         } else {
1956                 if (options.machinereadable){
1957                         printf(":Name:Level:\n");
1958                         printf(":%s:%d:\n",get_debug_by_level(level),level);
1959                 } else {
1960                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1961                 }
1962         }
1963         return 0;
1964 }
1965
1966
1967 /*
1968   set debug level on a node or all nodes
1969  */
1970 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1971 {
1972         int ret;
1973         int32_t level;
1974
1975         if (argc < 1) {
1976                 usage();
1977         }
1978
1979         if (isalpha(argv[0][0])) { 
1980                 level = get_debug_by_desc(argv[0]);
1981         } else {
1982                 level = strtol(argv[0], NULL, 0);
1983         }
1984
1985         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1986         if (ret != 0) {
1987                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1988         }
1989         return 0;
1990 }
1991
1992
1993 /*
1994   freeze a node
1995  */
1996 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1997 {
1998         int ret;
1999
2000         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
2001         if (ret != 0) {
2002                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
2003         }               
2004         return 0;
2005 }
2006
2007 /*
2008   thaw a node
2009  */
2010 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
2011 {
2012         int ret;
2013
2014         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
2015         if (ret != 0) {
2016                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
2017         }               
2018         return 0;
2019 }
2020
2021
2022 /*
2023   attach to a database
2024  */
2025 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
2026 {
2027         const char *db_name;
2028         struct ctdb_db_context *ctdb_db;
2029
2030         if (argc < 1) {
2031                 usage();
2032         }
2033         db_name = argv[0];
2034
2035         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
2036         if (ctdb_db == NULL) {
2037                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
2038                 return -1;
2039         }
2040
2041         return 0;
2042 }
2043
2044 /*
2045   run an eventscript on a node
2046  */
2047 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
2048 {
2049         TDB_DATA data;
2050         int ret;
2051         int32_t res;
2052         char *errmsg;
2053         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2054
2055         if (argc != 1) {
2056                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2057                 return -1;
2058         }
2059
2060         data.dptr = (unsigned char *)discard_const(argv[0]);
2061         data.dsize = strlen((char *)data.dptr) + 1;
2062
2063         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
2064
2065         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
2066                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
2067         if (ret != 0 || res != 0) {
2068                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
2069                 talloc_free(tmp_ctx);
2070                 return -1;
2071         }
2072         talloc_free(tmp_ctx);
2073         return 0;
2074 }
2075
2076 #define DB_VERSION 1
2077 #define MAX_DB_NAME 64
2078 struct db_file_header {
2079         unsigned long version;
2080         time_t timestamp;
2081         unsigned long persistent;
2082         unsigned long size;
2083         const char name[MAX_DB_NAME];
2084 };
2085
2086 struct backup_data {
2087         struct ctdb_marshall_buffer *records;
2088         uint32_t len;
2089         uint32_t total;
2090         bool traverse_error;
2091 };
2092
2093 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
2094 {
2095         struct backup_data *bd = talloc_get_type(private, struct backup_data);
2096         struct ctdb_rec_data *rec;
2097
2098         /* add the record */
2099         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
2100         if (rec == NULL) {
2101                 bd->traverse_error = true;
2102                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
2103                 return -1;
2104         }
2105         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
2106         if (bd->records == NULL) {
2107                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
2108                 bd->traverse_error = true;
2109                 return -1;
2110         }
2111         bd->records->count++;
2112         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
2113         bd->len += rec->length;
2114         talloc_free(rec);
2115
2116         bd->total++;
2117         return 0;
2118 }
2119
2120 /*
2121  * backup a database to a file 
2122  */
2123 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
2124 {
2125         int i, ret;
2126         struct ctdb_dbid_map *dbmap=NULL;
2127         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2128         struct db_file_header dbhdr;
2129         struct ctdb_db_context *ctdb_db;
2130         struct backup_data *bd;
2131         int fh;
2132
2133         if (argc != 2) {
2134                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2135                 return -1;
2136         }
2137
2138         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
2139         if (ret != 0) {
2140                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
2141                 return ret;
2142         }
2143
2144         for(i=0;i<dbmap->num;i++){
2145                 const char *name;
2146
2147                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
2148                 if(!strcmp(argv[0], name)){
2149                         talloc_free(discard_const(name));
2150                         break;
2151                 }
2152                 talloc_free(discard_const(name));
2153         }
2154         if (i == dbmap->num) {
2155                 DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
2156                 talloc_free(tmp_ctx);
2157                 return -1;
2158         }
2159
2160
2161         ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
2162         if (ctdb_db == NULL) {
2163                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
2164                 return -1;
2165         }
2166
2167
2168         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
2169         if (ret == -1) {
2170                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
2171                 talloc_free(tmp_ctx);
2172                 return -1;
2173         }
2174
2175
2176         bd = talloc_zero(tmp_ctx, struct backup_data);
2177         if (bd == NULL) {
2178                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
2179                 talloc_free(tmp_ctx);
2180                 return -1;
2181         }
2182
2183         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
2184         if (bd->records == NULL) {
2185                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
2186                 talloc_free(tmp_ctx);
2187                 return -1;
2188         }
2189
2190         bd->len = offsetof(struct ctdb_marshall_buffer, data);
2191         bd->records->db_id = ctdb_db->db_id;
2192         /* traverse the database collecting all records */
2193         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
2194             bd->traverse_error) {
2195                 DEBUG(DEBUG_ERR,("Traverse error\n"));
2196                 talloc_free(tmp_ctx);
2197                 return -1;              
2198         }
2199
2200         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
2201
2202
2203         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
2204         if (fh == -1) {
2205                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
2206                 talloc_free(tmp_ctx);
2207                 return -1;
2208         }
2209
2210         dbhdr.version = DB_VERSION;
2211         dbhdr.timestamp = time(NULL);
2212         dbhdr.persistent = dbmap->dbs[i].persistent;
2213         dbhdr.size = bd->len;
2214         if (strlen(argv[0]) >= MAX_DB_NAME) {
2215                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
2216                 talloc_free(tmp_ctx);
2217                 return -1;
2218         }
2219         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
2220         write(fh, &dbhdr, sizeof(dbhdr));
2221         write(fh, bd->records, bd->len);
2222
2223         close(fh);
2224         talloc_free(tmp_ctx);
2225         return 0;
2226 }
2227
2228 /*
2229  * restore a database from a file 
2230  */
2231 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
2232 {
2233         int ret;
2234         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2235         TDB_DATA outdata;
2236         TDB_DATA data;
2237         struct db_file_header dbhdr;
2238         struct ctdb_db_context *ctdb_db;
2239         struct ctdb_node_map *nodemap=NULL;
2240         struct ctdb_vnn_map *vnnmap=NULL;
2241         int fh;
2242         struct ctdb_control_wipe_database w;
2243         uint32_t *nodes;
2244         uint32_t generation;
2245         struct tm *tm;
2246         char tbuf[100];
2247
2248         if (argc != 1) {
2249                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
2250                 return -1;
2251         }
2252
2253         fh = open(argv[0], O_RDONLY);
2254         if (fh == -1) {
2255                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
2256                 talloc_free(tmp_ctx);
2257                 return -1;
2258         }
2259
2260         read(fh, &dbhdr, sizeof(dbhdr));
2261         if (dbhdr.version != DB_VERSION) {
2262                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
2263                 talloc_free(tmp_ctx);
2264                 return -1;
2265         }
2266
2267         outdata.dsize = dbhdr.size;
2268         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
2269         if (outdata.dptr == NULL) {
2270                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
2271                 close(fh);
2272                 talloc_free(tmp_ctx);
2273                 return -1;
2274         }               
2275         read(fh, outdata.dptr, outdata.dsize);
2276         close(fh);
2277
2278         tm = localtime(&dbhdr.timestamp);
2279         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
2280         printf("Restoring database '%s' from backup @ %s\n",
2281                 dbhdr.name, tbuf);
2282
2283
2284         ctdb_db = ctdb_attach(ctdb, dbhdr.name, dbhdr.persistent, 0);
2285         if (ctdb_db == NULL) {
2286                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbhdr.name));
2287                 talloc_free(tmp_ctx);
2288                 return -1;
2289         }
2290
2291         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2292         if (ret != 0) {
2293                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2294                 talloc_free(tmp_ctx);
2295                 return ret;
2296         }
2297
2298
2299         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
2300         if (ret != 0) {
2301                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
2302                 talloc_free(tmp_ctx);
2303                 return ret;
2304         }
2305
2306         /* freeze all nodes */
2307         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2308         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
2309                                         nodes, TIMELIMIT(),
2310                                         false, tdb_null,
2311                                         NULL, NULL,
2312                                         NULL) != 0) {
2313                 DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
2314                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2315                 talloc_free(tmp_ctx);
2316                 return -1;
2317         }
2318
2319         generation = vnnmap->generation;
2320         data.dptr = (void *)&generation;
2321         data.dsize = sizeof(generation);
2322
2323         /* start a cluster wide transaction */
2324         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2325         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
2326                                         nodes,
2327                                         TIMELIMIT(), false, data,
2328                                         NULL, NULL,
2329                                         NULL) != 0) {
2330                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
2331                 return -1;
2332         }
2333
2334
2335         w.db_id = ctdb_db->db_id;
2336         w.transaction_id = generation;
2337
2338         data.dptr = (void *)&w;
2339         data.dsize = sizeof(w);
2340
2341         /* wipe all the remote databases. */
2342         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2343         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
2344                                         nodes,
2345                                         TIMELIMIT(), false, data,
2346                                         NULL, NULL,
2347                                         NULL) != 0) {
2348                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
2349                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2350                 talloc_free(tmp_ctx);
2351                 return -1;
2352         }
2353         
2354         /* push the database */
2355         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2356         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
2357                                         nodes,
2358                                         TIMELIMIT(), false, outdata,
2359                                         NULL, NULL,
2360                                         NULL) != 0) {
2361                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
2362                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2363                 talloc_free(tmp_ctx);
2364                 return -1;
2365         }
2366
2367         data.dptr = (void *)&generation;
2368         data.dsize = sizeof(generation);
2369
2370         /* commit all the changes */
2371         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
2372                                         nodes,
2373                                         TIMELIMIT(), false, data,
2374                                         NULL, NULL,
2375                                         NULL) != 0) {
2376                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
2377                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2378                 talloc_free(tmp_ctx);
2379                 return -1;
2380         }
2381
2382
2383         /* thaw all nodes */
2384         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
2385         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
2386                                         nodes, TIMELIMIT(),
2387                                         false, tdb_null,
2388                                         NULL, NULL,
2389                                         NULL) != 0) {
2390                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
2391                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
2392                 talloc_free(tmp_ctx);
2393                 return -1;
2394         }
2395
2396
2397         talloc_free(tmp_ctx);
2398         return 0;
2399 }
2400
2401 /*
2402  * set flags of a node in the nodemap
2403  */
2404 static int control_setflags(struct ctdb_context *ctdb, int argc, const char **argv)
2405 {
2406         int ret;
2407         int32_t status;
2408         int node;
2409         int flags;
2410         TDB_DATA data;
2411         struct ctdb_node_flag_change c;
2412
2413         if (argc != 2) {
2414                 usage();
2415                 return -1;
2416         }
2417
2418         if (sscanf(argv[0], "%d", &node) != 1) {
2419                 DEBUG(DEBUG_ERR, ("Badly formed node\n"));
2420                 usage();
2421                 return -1;
2422         }
2423         if (sscanf(argv[1], "0x%x", &flags) != 1) {
2424                 DEBUG(DEBUG_ERR, ("Badly formed flags\n"));
2425                 usage();
2426                 return -1;
2427         }
2428
2429         c.pnn       = node;
2430         c.old_flags = 0;
2431         c.new_flags = flags;
2432
2433         data.dsize = sizeof(c);
2434         data.dptr = (unsigned char *)&c;
2435
2436         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_MODIFY_FLAGS, 0, 
2437                            data, NULL, NULL, &status, NULL, NULL);
2438         if (ret != 0 || status != 0) {
2439                 DEBUG(DEBUG_ERR,("Failed to modify flags\n"));
2440                 return -1;
2441         }
2442         return 0;
2443 }
2444
2445 /*
2446   dump memory usage
2447  */
2448 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2449 {
2450         TDB_DATA data;
2451         int ret;
2452         int32_t res;
2453         char *errmsg;
2454         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2455         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
2456                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
2457         if (ret != 0 || res != 0) {
2458                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
2459                 talloc_free(tmp_ctx);
2460                 return -1;
2461         }
2462         write(1, data.dptr, data.dsize);
2463         talloc_free(tmp_ctx);
2464         return 0;
2465 }
2466
2467 /*
2468   handler for memory dumps
2469 */
2470 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
2471                              TDB_DATA data, void *private_data)
2472 {
2473         write(1, data.dptr, data.dsize);
2474         exit(0);
2475 }
2476
2477 /*
2478   dump memory usage on the recovery daemon
2479  */
2480 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
2481 {
2482         int ret;
2483         TDB_DATA data;
2484         struct rd_memdump_reply rd;
2485
2486         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2487         if (rd.pnn == -1) {
2488                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
2489                 return -1;
2490         }
2491         rd.srvid = getpid();
2492
2493         /* register a message port for receiveing the reply so that we
2494            can receive the reply
2495         */
2496         ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
2497
2498
2499         data.dptr = (uint8_t *)&rd;
2500         data.dsize = sizeof(rd);
2501
2502         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
2503         if (ret != 0) {
2504                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
2505                 return -1;
2506         }
2507
2508         /* this loop will terminate when we have received the reply */
2509         while (1) {     
2510                 event_loop_once(ctdb->ev);
2511         }
2512
2513         return 0;
2514 }
2515
2516 /*
2517   list all nodes in the cluster
2518  */
2519 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
2520 {
2521         int i, ret;
2522         struct ctdb_node_map *nodemap=NULL;
2523
2524         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
2525         if (ret != 0) {
2526                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2527                 return ret;
2528         }
2529
2530         for(i=0;i<nodemap->num;i++){
2531                 printf("%s\n", ctdb_addr_to_str(&nodemap->nodes[i].addr));
2532         }
2533
2534         return 0;
2535 }
2536
2537 /*
2538   reload the nodes file on the local node
2539  */
2540 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
2541 {
2542         int i, ret;
2543         int mypnn;
2544         struct ctdb_node_map *nodemap=NULL;
2545
2546         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
2547         if (mypnn == -1) {
2548                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
2549                 return -1;
2550         }
2551
2552         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
2553         if (ret != 0) {
2554                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
2555                 return ret;
2556         }
2557
2558         /* reload the nodes file on all remote nodes */
2559         for (i=0;i<nodemap->num;i++) {
2560                 if (nodemap->nodes[i].pnn == mypnn) {
2561                         continue;
2562                 }
2563                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
2564                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
2565                         nodemap->nodes[i].pnn);
2566                 if (ret != 0) {
2567                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
2568                 }
2569         }
2570
2571         /* reload the nodes file on the local node */
2572         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
2573         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
2574         if (ret != 0) {
2575                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
2576         }
2577
2578         /* initiate a recovery */
2579         control_recover(ctdb, argc, argv);
2580
2581         return 0;
2582 }
2583
2584
2585 static const struct {
2586         const char *name;
2587         int (*fn)(struct ctdb_context *, int, const char **);
2588         bool auto_all;
2589         const char *msg;
2590         const char *args;
2591 } ctdb_commands[] = {
2592 #ifdef CTDB_VERS
2593         { "version",         control_version,           true,  "show version of ctdb" },
2594 #endif
2595         { "status",          control_status,            true,  "show node status" },
2596         { "uptime",          control_uptime,            true,  "show node uptime" },
2597         { "ping",            control_ping,              true,  "ping all nodes" },
2598         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
2599         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
2600         { "listvars",        control_listvars,          true,  "list tunable variables"},
2601         { "statistics",      control_statistics,        false, "show statistics" },
2602         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
2603         { "ip",              control_ip,                false,  "show which public ip's that ctdb manages" },
2604         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
2605         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
2606         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
2607         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
2608         { "getcapabilities", control_getcapabilities,   true,  "show node capabilities" },
2609         { "pnn",             control_pnn,               true,  "show the pnn of the currnet node" },
2610         { "lvs",             control_lvs,               true,  "show lvs configuration" },
2611         { "lvsmaster",       control_lvsmaster,         true,  "show which node is the lvs master" },
2612         { "disablemonitor",      control_disable_monmode,        true,  "set monitoring mode to DISABLE" },
2613         { "enablemonitor",      control_enable_monmode,        true,  "set monitoring mode to ACTIVE" },
2614         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
2615         { "getdebug",        control_getdebug,          true,  "get debug level" },
2616         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
2617         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
2618         { "rddumpmemory",    control_rddumpmemory,      true,  "dump memory map from the recovery daemon to stdout" },
2619         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
2620         { "disable",         control_disable,           true,  "disable a nodes public IP" },
2621         { "enable",          control_enable,            true,  "enable a nodes public IP" },
2622         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
2623         { "unban",           control_unban,             true,  "unban a node from the cluster" },
2624         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
2625         { "recover",         control_recover,           true,  "force recovery" },
2626         { "freeze",          control_freeze,            true,  "freeze all databases" },
2627         { "thaw",            control_thaw,              true,  "thaw all databases" },
2628         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
2629         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
2630         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
2631         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
2632         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
2633
2634         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
2635         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
2636         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
2637         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
2638         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
2639         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
2640         { "listnodes",       control_listnodes,         false, "list all nodes in the cluster"},
2641         { "reloadnodes",     control_reload_nodes_file,         false, "reload the nodes file and restart the transport on all nodes"},
2642         { "moveip",          control_moveip,            false, "move/failover an ip address to another node", "<ip> <node>"},
2643         { "addip",           control_addip,             true, "add a ip address to a node", "<ip/mask> <iface>"},
2644         { "delip",           control_delip,             false, "delete an ip address from a node", "<ip>"},
2645         { "eventscript",     control_eventscript,       true, "run the eventscript with the given parameters on a node", "<arguments>"},
2646         { "backupdb",        control_backupdb,          false, "backup the database into a file.", "<database> <file>"},
2647         { "restoredb",        control_restoredb,          false, "restore the database from a file.", "<file>"},
2648         { "recmaster",        control_recmaster,          false, "show the pnn for the recovery master."},
2649         { "setflags",        control_setflags,            false, "set flags for a node in the nodemap.", "<node> <flags>"},
2650 };
2651
2652 /*
2653   show usage message
2654  */
2655 static void usage(void)
2656 {
2657         int i;
2658         printf(
2659 "Usage: ctdb [options] <control>\n" \
2660 "Options:\n" \
2661 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
2662 "   -Y                 generate machinereadable output\n"
2663 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
2664         printf("Controls:\n");
2665         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2666                 printf("  %-15s %-27s  %s\n", 
2667                        ctdb_commands[i].name, 
2668                        ctdb_commands[i].args?ctdb_commands[i].args:"",
2669                        ctdb_commands[i].msg);
2670         }
2671         exit(1);
2672 }
2673
2674
2675 static void ctdb_alarm(int sig)
2676 {
2677         printf("Maximum runtime exceeded - exiting\n");
2678         _exit(ERR_TIMEOUT);
2679 }
2680
2681 /*
2682   main program
2683 */
2684 int main(int argc, const char *argv[])
2685 {
2686         struct ctdb_context *ctdb;
2687         char *nodestring = NULL;
2688         struct poptOption popt_options[] = {
2689                 POPT_AUTOHELP
2690                 POPT_CTDB_CMDLINE
2691                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
2692                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
2693                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
2694                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
2695                 POPT_TABLEEND
2696         };
2697         int opt;
2698         const char **extra_argv;
2699         int extra_argc = 0;
2700         int ret=-1, i;
2701         poptContext pc;
2702         struct event_context *ev;
2703         const char *control;
2704
2705         setlinebuf(stdout);
2706         
2707         /* set some defaults */
2708         options.maxruntime = 0;
2709         options.timelimit = 3;
2710         options.pnn = CTDB_CURRENT_NODE;
2711
2712         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
2713
2714         while ((opt = poptGetNextOpt(pc)) != -1) {
2715                 switch (opt) {
2716                 default:
2717                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
2718                                 poptBadOption(pc, 0), poptStrerror(opt)));
2719                         exit(1);
2720                 }
2721         }
2722
2723         /* setup the remaining options for the main program to use */
2724         extra_argv = poptGetArgs(pc);
2725         if (extra_argv) {
2726                 extra_argv++;
2727                 while (extra_argv[extra_argc]) extra_argc++;
2728         }
2729
2730         if (extra_argc < 1) {
2731                 usage();
2732         }
2733
2734         if (options.maxruntime == 0) {
2735                 const char *ctdb_timeout;
2736                 ctdb_timeout = getenv("CTDB_TIMEOUT");
2737                 if (ctdb_timeout != NULL) {
2738                         options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
2739                 }
2740         }
2741         if (options.maxruntime != 0) {
2742                 signal(SIGALRM, ctdb_alarm);
2743                 alarm(options.maxruntime);
2744         }
2745
2746         /* setup the node number to contact */
2747         if (nodestring != NULL) {
2748                 if (strcmp(nodestring, "all") == 0) {
2749                         options.pnn = CTDB_BROADCAST_ALL;
2750                 } else {
2751                         options.pnn = strtoul(nodestring, NULL, 0);
2752                 }
2753         }
2754
2755         control = extra_argv[0];
2756
2757         ev = event_context_init(NULL);
2758
2759         /* initialise ctdb */
2760         ctdb = ctdb_cmdline_client(ev);
2761         if (ctdb == NULL) {
2762                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
2763                 exit(1);
2764         }
2765
2766         /* verify the node exists */
2767         verify_node(ctdb);
2768
2769         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
2770                 if (strcmp(control, ctdb_commands[i].name) == 0) {
2771                         int j;
2772
2773                         if (options.pnn == CTDB_CURRENT_NODE) {
2774                                 int pnn;
2775                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
2776                                 if (pnn == -1) {
2777                                         return -1;
2778                                 }
2779                                 options.pnn = pnn;
2780                         }
2781
2782                         if (ctdb_commands[i].auto_all && 
2783                             options.pnn == CTDB_BROADCAST_ALL) {
2784                                 uint32_t *nodes;
2785                                 uint32_t num_nodes;
2786                                 ret = 0;
2787
2788                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2789                                 CTDB_NO_MEMORY(ctdb, nodes);
2790         
2791                                 for (j=0;j<num_nodes;j++) {
2792                                         options.pnn = nodes[j];
2793                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2794                                 }
2795                                 talloc_free(nodes);
2796                         } else {
2797                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2798                         }
2799                         break;
2800                 }
2801         }
2802
2803         if (i == ARRAY_SIZE(ctdb_commands)) {
2804                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
2805                 exit(1);
2806         }
2807
2808         return ret;
2809 }