add a parameter for the tdb-flags to the client function
[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
33 static void usage(void);
34
35 static struct {
36         int timelimit;
37         uint32_t pnn;
38         int machinereadable;
39         int maxruntime;
40 } options;
41
42 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
43
44 #ifdef CTDB_VERS
45 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
46 {
47 #define STR(x) #x
48 #define XSTR(x) STR(x)
49         printf("CTDB version: %s\n", XSTR(CTDB_VERS));
50         return 0;
51 }
52 #endif
53
54 /*
55   see if a process exists
56  */
57 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
58 {
59         uint32_t pnn, pid;
60         int ret;
61         if (argc < 1) {
62                 usage();
63         }
64
65         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
66                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
67                 return -1;
68         }
69
70         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
71         if (ret == 0) {
72                 printf("%u:%u exists\n", pnn, pid);
73         } else {
74                 printf("%u:%u does not exist\n", pnn, pid);
75         }
76         return ret;
77 }
78
79 /*
80   display statistics structure
81  */
82 static void show_statistics(struct ctdb_statistics *s)
83 {
84         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
85         int i;
86         const char *prefix=NULL;
87         int preflen=0;
88         const struct {
89                 const char *name;
90                 uint32_t offset;
91         } fields[] = {
92 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
93                 STATISTICS_FIELD(num_clients),
94                 STATISTICS_FIELD(frozen),
95                 STATISTICS_FIELD(recovering),
96                 STATISTICS_FIELD(client_packets_sent),
97                 STATISTICS_FIELD(client_packets_recv),
98                 STATISTICS_FIELD(node_packets_sent),
99                 STATISTICS_FIELD(node_packets_recv),
100                 STATISTICS_FIELD(keepalive_packets_sent),
101                 STATISTICS_FIELD(keepalive_packets_recv),
102                 STATISTICS_FIELD(node.req_call),
103                 STATISTICS_FIELD(node.reply_call),
104                 STATISTICS_FIELD(node.req_dmaster),
105                 STATISTICS_FIELD(node.reply_dmaster),
106                 STATISTICS_FIELD(node.reply_error),
107                 STATISTICS_FIELD(node.req_message),
108                 STATISTICS_FIELD(node.req_control),
109                 STATISTICS_FIELD(node.reply_control),
110                 STATISTICS_FIELD(client.req_call),
111                 STATISTICS_FIELD(client.req_message),
112                 STATISTICS_FIELD(client.req_control),
113                 STATISTICS_FIELD(timeouts.call),
114                 STATISTICS_FIELD(timeouts.control),
115                 STATISTICS_FIELD(timeouts.traverse),
116                 STATISTICS_FIELD(total_calls),
117                 STATISTICS_FIELD(pending_calls),
118                 STATISTICS_FIELD(lockwait_calls),
119                 STATISTICS_FIELD(pending_lockwait_calls),
120                 STATISTICS_FIELD(childwrite_calls),
121                 STATISTICS_FIELD(pending_childwrite_calls),
122                 STATISTICS_FIELD(memory_used),
123                 STATISTICS_FIELD(max_hop_count),
124         };
125         printf("CTDB version %u\n", CTDB_VERSION);
126         for (i=0;i<ARRAY_SIZE(fields);i++) {
127                 if (strchr(fields[i].name, '.')) {
128                         preflen = strcspn(fields[i].name, ".")+1;
129                         if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
130                                 prefix = fields[i].name;
131                                 printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
132                         }
133                 } else {
134                         preflen = 0;
135                 }
136                 printf(" %*s%-22s%*s%10u\n", 
137                        preflen?4:0, "",
138                        fields[i].name+preflen, 
139                        preflen?0:4, "",
140                        *(uint32_t *)(fields[i].offset+(uint8_t *)s));
141         }
142         printf(" %-30s     %.6f sec\n", "max_call_latency", s->max_call_latency);
143         printf(" %-30s     %.6f sec\n", "max_lockwait_latency", s->max_lockwait_latency);
144         printf(" %-30s     %.6f sec\n", "max_childwrite_latency", s->max_childwrite_latency);
145         talloc_free(tmp_ctx);
146 }
147
148 /*
149   display remote ctdb statistics combined from all nodes
150  */
151 static int control_statistics_all(struct ctdb_context *ctdb)
152 {
153         int ret, i;
154         struct ctdb_statistics statistics;
155         uint32_t *nodes;
156         uint32_t num_nodes;
157
158         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
159         CTDB_NO_MEMORY(ctdb, nodes);
160         
161         ZERO_STRUCT(statistics);
162
163         for (i=0;i<num_nodes;i++) {
164                 struct ctdb_statistics s1;
165                 int j;
166                 uint32_t *v1 = (uint32_t *)&s1;
167                 uint32_t *v2 = (uint32_t *)&statistics;
168                 uint32_t num_ints = 
169                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
170                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
171                 if (ret != 0) {
172                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
173                         return ret;
174                 }
175                 for (j=0;j<num_ints;j++) {
176                         v2[j] += v1[j];
177                 }
178                 statistics.max_hop_count = 
179                         MAX(statistics.max_hop_count, s1.max_hop_count);
180                 statistics.max_call_latency = 
181                         MAX(statistics.max_call_latency, s1.max_call_latency);
182                 statistics.max_lockwait_latency = 
183                         MAX(statistics.max_lockwait_latency, s1.max_lockwait_latency);
184         }
185         talloc_free(nodes);
186         printf("Gathered statistics for %u nodes\n", num_nodes);
187         show_statistics(&statistics);
188         return 0;
189 }
190
191 /*
192   display remote ctdb statistics
193  */
194 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
195 {
196         int ret;
197         struct ctdb_statistics statistics;
198
199         if (options.pnn == CTDB_BROADCAST_ALL) {
200                 return control_statistics_all(ctdb);
201         }
202
203         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
204         if (ret != 0) {
205                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
206                 return ret;
207         }
208         show_statistics(&statistics);
209         return 0;
210 }
211
212
213 /*
214   reset remote ctdb statistics
215  */
216 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
217 {
218         int ret;
219
220         ret = ctdb_statistics_reset(ctdb, options.pnn);
221         if (ret != 0) {
222                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
223                 return ret;
224         }
225         return 0;
226 }
227
228
229 /*
230   display uptime of remote node
231  */
232 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
233 {
234         int ret;
235         int mypnn;
236         struct ctdb_uptime *uptime = NULL;
237         int tmp, days, hours, minutes, seconds;
238
239         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
240         if (mypnn == -1) {
241                 return -1;
242         }
243
244         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
245         if (ret != 0) {
246                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
247                 return ret;
248         }
249
250         if (options.machinereadable){
251                 printf(":Current Node Time:Ctdb Start Time:Last Recovery Time:\n");
252                 printf(":%u:%u:%u:\n",
253                         (unsigned int)uptime->current_time.tv_sec,
254                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
255                         (unsigned int)uptime->last_recovery_time.tv_sec);
256                 return 0;
257         }
258
259         printf("Current time of node  : %s", ctime(&uptime->current_time.tv_sec));
260
261         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
262         seconds = tmp%60;
263         tmp    /= 60;
264         minutes = tmp%60;
265         tmp    /= 60;
266         hours   = tmp%24;
267         tmp    /= 24;
268         days    = tmp;
269         printf("Ctdbd start time      : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
270
271         tmp = uptime->current_time.tv_sec - uptime->last_recovery_time.tv_sec;
272         seconds = tmp%60;
273         tmp    /= 60;
274         minutes = tmp%60;
275         tmp    /= 60;
276         hours   = tmp%24;
277         tmp    /= 24;
278         days    = tmp;
279         printf("Time of last recovery : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_time.tv_sec));
280
281         return 0;
282 }
283
284 /*
285   display remote ctdb status
286  */
287 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
288 {
289         int i, ret;
290         struct ctdb_vnn_map *vnnmap=NULL;
291         struct ctdb_node_map *nodemap=NULL;
292         uint32_t recmode, recmaster;
293         int mypnn;
294
295         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
296         if (mypnn == -1) {
297                 return -1;
298         }
299
300         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
301         if (ret != 0) {
302                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
303                 return ret;
304         }
305
306         if(options.machinereadable){
307                 printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:\n");
308                 for(i=0;i<nodemap->num;i++){
309                         printf(":%d:%s:%d:%d:%d:%d:\n", nodemap->nodes[i].pnn,
310                                 inet_ntoa(nodemap->nodes[i].sin.sin_addr),
311                                !!(nodemap->nodes[i].flags&NODE_FLAGS_DISCONNECTED),
312                                !!(nodemap->nodes[i].flags&NODE_FLAGS_BANNED),
313                                !!(nodemap->nodes[i].flags&NODE_FLAGS_PERMANENTLY_DISABLED),
314                                !!(nodemap->nodes[i].flags&NODE_FLAGS_UNHEALTHY));
315                 }
316                 return 0;
317         }
318
319         printf("Number of nodes:%d\n", nodemap->num);
320         for(i=0;i<nodemap->num;i++){
321                 static const struct {
322                         uint32_t flag;
323                         const char *name;
324                 } flag_names[] = {
325                         { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
326                         { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
327                         { NODE_FLAGS_BANNED,                "BANNED" },
328                         { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
329                 };
330                 char *flags_str = NULL;
331                 int j;
332                 for (j=0;j<ARRAY_SIZE(flag_names);j++) {
333                         if (nodemap->nodes[i].flags & flag_names[j].flag) {
334                                 if (flags_str == NULL) {
335                                         flags_str = talloc_strdup(ctdb, flag_names[j].name);
336                                 } else {
337                                         flags_str = talloc_asprintf_append(flags_str, "|%s",
338                                                                            flag_names[j].name);
339                                 }
340                                 CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
341                         }
342                 }
343                 if (flags_str == NULL) {
344                         flags_str = talloc_strdup(ctdb, "OK");
345                         CTDB_NO_MEMORY_FATAL(ctdb, flags_str);
346                 }
347                 printf("pnn:%d %-16s %s%s\n", nodemap->nodes[i].pnn,
348                        inet_ntoa(nodemap->nodes[i].sin.sin_addr),
349                        flags_str,
350                        nodemap->nodes[i].pnn == mypnn?" (THIS NODE)":"");
351                 talloc_free(flags_str);
352         }
353
354         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &vnnmap);
355         if (ret != 0) {
356                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
357                 return ret;
358         }
359         if (vnnmap->generation == INVALID_GENERATION) {
360                 printf("Generation:INVALID\n");
361         } else {
362                 printf("Generation:%d\n",vnnmap->generation);
363         }
364         printf("Size:%d\n",vnnmap->size);
365         for(i=0;i<vnnmap->size;i++){
366                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
367         }
368
369         ret = ctdb_ctrl_getrecmode(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmode);
370         if (ret != 0) {
371                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
372                 return ret;
373         }
374         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
375
376         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
377         if (ret != 0) {
378                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
379                 return ret;
380         }
381         printf("Recovery master:%d\n",recmaster);
382
383         return 0;
384 }
385
386 /*
387   get a list of all tickles for this pnn
388  */
389 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
390 {
391         struct ctdb_control_tcp_tickle_list *list;
392         struct sockaddr_in ip;
393         int i, ret;
394
395         if (argc < 1) {
396                 usage();
397         }
398
399         ip.sin_family = AF_INET;
400         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
401                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
402                 return -1;
403         }
404
405         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &ip, &list);
406         if (ret == -1) {
407                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
408                 return -1;
409         }
410
411         printf("Tickles for ip:%s\n", inet_ntoa(list->ip.sin_addr));
412         printf("Num tickles:%u\n", list->tickles.num);
413         for (i=0;i<list->tickles.num;i++) {
414                 printf("SRC: %s:%u   ", inet_ntoa(list->tickles.connections[i].saddr.sin_addr), ntohs(list->tickles.connections[i].saddr.sin_port));
415                 printf("DST: %s:%u\n", inet_ntoa(list->tickles.connections[i].daddr.sin_addr), ntohs(list->tickles.connections[i].daddr.sin_port));
416         }
417
418         talloc_free(list);
419         
420         return 0;
421 }
422
423 /* send a release ip to all nodes */
424 static int control_send_release(struct ctdb_context *ctdb, uint32_t pnn,
425 struct sockaddr_in *sin)
426 {
427         int ret;
428         struct ctdb_public_ip pip;
429         TDB_DATA data;
430         struct ctdb_node_map *nodemap=NULL;
431
432         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
433         if (ret != 0) {
434                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
435                 return ret;
436         }
437
438         /* send a moveip message to the recovery master */
439         pip.pnn = pnn;
440         pip.sin.sin_family = AF_INET;
441         pip.sin.sin_addr   = sin->sin_addr;
442         data.dsize = sizeof(pip);
443         data.dptr = (unsigned char *)&pip;
444
445
446         /* send release ip to all nodes */
447         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
448                         list_of_active_nodes(ctdb, nodemap, ctdb, true),
449                         TIMELIMIT(), false, data, NULL) != 0) {
450                 DEBUG(DEBUG_ERR, (__location__ " Unable to send 'ReleaseIP' to all nodes.\n"));
451                 return -1;
452         }
453
454         return 0;
455 }
456
457 /*
458   move/failover an ip address to a specific node
459  */
460 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
461 {
462         uint32_t pnn;
463         struct sockaddr_in ip;
464         uint32_t value;
465         struct ctdb_all_public_ips *ips;
466         int i, ret;
467
468         if (argc < 2) {
469                 usage();
470         }
471
472         ip.sin_family = AF_INET;
473         if (inet_aton(argv[0], &ip.sin_addr) == 0) {
474                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
475                 return -1;
476         }
477
478
479         if (sscanf(argv[1], "%u", &pnn) != 1) {
480                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
481                 return -1;
482         }
483
484         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "DeterministicIPs", &value);
485         if (ret == -1) {
486                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'DeterministicIPs' from local node\n"));
487                 return -1;
488         }
489         if (value != 0) {
490                 DEBUG(DEBUG_ERR, ("The tunable 'DeterministicIPs' is set. You can only move ip addresses when this feature is disabled\n"));
491                 return -1;
492         }
493
494         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, "NoIPFailback", &value);
495         if (ret == -1) {
496                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable 'NoIPFailback' from local node\n"));
497                 return -1;
498         }
499         if (value == 0) {
500                 DEBUG(DEBUG_ERR, ("The tunable 'NoIPFailback' is NOT set. You can only move ip addresses when this feature is enabled\n"));
501                 return -1;
502         }
503
504         /* read the public ip list from the node */
505         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
506         if (ret != 0) {
507                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
508                 return -1;
509         }
510
511         for (i=0;i<ips->num;i++) {
512                 if (ctdb_same_ip(&ip, &ips->ips[i].sin)) {
513                         break;
514                 }
515         }
516         if (i==ips->num) {
517                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
518                         pnn, inet_ntoa(ip.sin_addr)));
519                 return -1;
520         }
521         if (ips->ips[i].pnn == pnn) {
522                 DEBUG(DEBUG_ERR, ("Host %u is already hosting '%s'\n",
523                         pnn, inet_ntoa(ips->ips[i].sin.sin_addr)));
524                 return -1;
525         }
526
527         ret = control_send_release(ctdb, pnn, &ips->ips[i].sin);
528         if (ret != 0) {
529                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));;
530                 return -1;
531         }
532
533         return 0;
534 }
535
536 struct node_ip {
537         uint32_t pnn;
538         struct sockaddr_in sin;
539 };
540
541 void getips_store_callback(void *param, void *data)
542 {
543         struct node_ip *node_ip = (struct node_ip *)data;
544         struct ctdb_all_public_ips *ips = param;
545         int i;
546
547         i = ips->num++;
548         ips->ips[i].pnn = node_ip->pnn;
549         ips->ips[i].sin = node_ip->sin;
550 }
551
552 void getips_count_callback(void *param, void *data)
553 {
554         uint32_t *count = param;
555
556         (*count)++;
557 }
558
559 static int
560 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
561 {
562         struct ctdb_all_public_ips *tmp_ips;
563         struct ctdb_node_map *nodemap=NULL;
564         trbt_tree_t *tree;
565         int i, j, len, ret;
566         uint32_t count;
567
568         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
569         if (ret != 0) {
570                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
571                 return ret;
572         }
573
574         tree = trbt_create(tmp_ctx, 0);
575
576         for(i=0;i<nodemap->num;i++){
577                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
578                         continue;
579                 }
580
581                 /* read the public ip list from this node */
582                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
583                 if (ret != 0) {
584                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
585                         return -1;
586                 }
587         
588                 for (j=0; j<tmp_ips->num;j++) {
589                         struct node_ip *node_ip;
590
591                         node_ip = talloc(tmp_ctx, struct node_ip);
592                         node_ip->pnn = tmp_ips->ips[j].pnn;
593                         node_ip->sin = tmp_ips->ips[j].sin;
594
595                         trbt_insert32(tree, tmp_ips->ips[j].sin.sin_addr.s_addr, node_ip);
596                 }
597                 talloc_free(tmp_ips);
598         }
599
600         /* traverse */
601         count = 0;
602         trbt_traversearray32(tree, 1, getips_count_callback, &count);
603
604         len = offsetof(struct ctdb_all_public_ips, ips) + 
605                 count*sizeof(struct ctdb_public_ip);
606         tmp_ips = talloc_zero_size(tmp_ctx, len);
607         trbt_traversearray32(tree, 1, getips_store_callback, tmp_ips);
608
609         *ips = tmp_ips;
610
611         return 0;
612 }
613
614
615 /* 
616  * scans all other nodes and returns a pnn for another node that can host this 
617  * ip address or -1
618  */
619 static int
620 find_other_host_for_public_ip(struct ctdb_context *ctdb, struct sockaddr_in *addr)
621 {
622         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
623         struct ctdb_all_public_ips *ips;
624         struct ctdb_node_map *nodemap=NULL;
625         int i, j, ret;
626
627         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
628         if (ret != 0) {
629                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
630                 talloc_free(tmp_ctx);
631                 return ret;
632         }
633
634         for(i=0;i<nodemap->num;i++){
635                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
636                         continue;
637                 }
638                 if (nodemap->nodes[i].pnn == options.pnn) {
639                         continue;
640                 }
641
642                 /* read the public ip list from this node */
643                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
644                 if (ret != 0) {
645                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
646                         return -1;
647                 }
648
649                 for (j=0;j<ips->num;j++) {
650                         if (ctdb_same_ip(addr, &ips->ips[j].sin)) {
651                                 talloc_free(tmp_ctx);
652                                 return nodemap->nodes[i].pnn;
653                         }
654                 }
655                 talloc_free(ips);
656         }
657
658         talloc_free(tmp_ctx);
659         return -1;
660 }
661
662 /*
663   add a public ip address to a node
664  */
665 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
666 {
667         int i, ret;
668         int len;
669         unsigned mask;
670         struct sockaddr_in addr;
671         struct ctdb_control_ip_iface *pub;
672         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
673         struct ctdb_all_public_ips *ips;
674
675         if (argc != 2) {
676                 talloc_free(tmp_ctx);
677                 usage();
678         }
679
680         if (!parse_ip_mask(argv[0], &addr, &mask)) {
681                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
682                 talloc_free(tmp_ctx);
683                 return -1;
684         }
685
686         ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
687         if (ret != 0) {
688                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
689                 talloc_free(tmp_ctx);
690                 return ret;
691         }
692
693
694         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
695         pub = talloc_size(tmp_ctx, len); 
696         CTDB_NO_MEMORY(ctdb, pub);
697
698         pub->sin   = addr;
699         pub->mask  = mask;
700         pub->len   = strlen(argv[1])+1;
701         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
702
703         ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
704         if (ret != 0) {
705                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u\n", options.pnn));
706                 talloc_free(tmp_ctx);
707                 return ret;
708         }
709
710
711         /* check if some other node is already serving this ip, if not,
712          * we will claim it
713          */
714         for (i=0;i<ips->num;i++) {
715                 if (ctdb_same_ip(&addr, &ips->ips[i].sin)) {
716                         break;
717                 }
718         }
719         /* no one has this ip so we claim it */
720         if (i == ips->num) {
721                 ret = control_send_release(ctdb, options.pnn, &addr);
722         } else {
723                 ret = control_send_release(ctdb, ips->ips[i].pnn, &addr);
724         }
725
726         if (ret != 0) {
727                 DEBUG(DEBUG_ERR, ("Failed to send 'change ip' to all nodes\n"));
728                 return -1;
729         }
730
731         talloc_free(tmp_ctx);
732         return 0;
733 }
734
735 /*
736   delete a public ip address from a node
737  */
738 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
739 {
740         int i, ret;
741         struct sockaddr_in addr;
742         struct ctdb_control_ip_iface pub;
743         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
744         struct ctdb_all_public_ips *ips;
745
746         if (argc != 1) {
747                 talloc_free(tmp_ctx);
748                 usage();
749         }
750
751         addr.sin_family = AF_INET;
752         if (inet_aton(argv[0], &addr.sin_addr) == 0) {
753                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
754                 return -1;
755         }
756
757         pub.sin   = addr;
758         pub.mask  = 0;
759         pub.len   = 0;
760
761         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
762         if (ret != 0) {
763                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
764                 talloc_free(tmp_ctx);
765                 return ret;
766         }
767         
768         for (i=0;i<ips->num;i++) {
769                 if (ctdb_same_ip(&addr, &ips->ips[i].sin)) {
770                         break;
771                 }
772         }
773
774         if (i==ips->num) {
775                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
776                         inet_ntoa(addr.sin_addr)));
777                 talloc_free(tmp_ctx);
778                 return -1;
779         }
780
781         if (ips->ips[i].pnn == options.pnn) {
782                 ret = find_other_host_for_public_ip(ctdb, &addr);
783                 if (ret != -1) {
784                         ret = control_send_release(ctdb, ret, &addr);
785                         if (ret != 0) {
786                                 DEBUG(DEBUG_ERR, ("Failed to migrate this ip to another node. Use moveip of recover to reassign this address to a node\n"));
787                         }
788                 }
789         }
790
791         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
792         if (ret != 0) {
793                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
794                 talloc_free(tmp_ctx);
795                 return ret;
796         }
797
798         talloc_free(tmp_ctx);
799         return 0;
800 }
801
802 /*
803   kill a tcp connection
804  */
805 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
806 {
807         int ret;
808         struct ctdb_control_killtcp killtcp;
809
810         if (argc < 2) {
811                 usage();
812         }
813
814         if (!parse_ip_port(argv[0], (ctdb_sock_addr *)&killtcp.src)) {
815                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
816                 return -1;
817         }
818
819         if (!parse_ip_port(argv[1], (ctdb_sock_addr *)&killtcp.dst)) {
820                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
821                 return -1;
822         }
823
824         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
825         if (ret != 0) {
826                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
827                 return ret;
828         }
829
830         return 0;
831 }
832
833
834 /*
835   send a gratious arp
836  */
837 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
838 {
839         int ret;
840         struct sockaddr_in sin;
841
842         if (argc < 2) {
843                 usage();
844         }
845
846         sin.sin_family = AF_INET;
847         if (inet_aton(argv[0], &sin.sin_addr) == 0) {
848                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
849                 return -1;
850         }
851
852         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &sin, argv[1]);
853         if (ret != 0) {
854                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
855                 return ret;
856         }
857
858         return 0;
859 }
860
861 /*
862   register a server id
863  */
864 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
865 {
866         int ret;
867         struct ctdb_server_id server_id;
868
869         if (argc < 3) {
870                 usage();
871         }
872
873         server_id.pnn       = strtoul(argv[0], NULL, 0);
874         server_id.type      = strtoul(argv[1], NULL, 0);
875         server_id.server_id = strtoul(argv[2], NULL, 0);
876
877         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
878         if (ret != 0) {
879                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
880                 return ret;
881         }
882         return -1;
883 }
884
885 /*
886   unregister a server id
887  */
888 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
889 {
890         int ret;
891         struct ctdb_server_id server_id;
892
893         if (argc < 3) {
894                 usage();
895         }
896
897         server_id.pnn       = strtoul(argv[0], NULL, 0);
898         server_id.type      = strtoul(argv[1], NULL, 0);
899         server_id.server_id = strtoul(argv[2], NULL, 0);
900
901         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
902         if (ret != 0) {
903                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
904                 return ret;
905         }
906         return -1;
907 }
908
909 /*
910   check if a server id exists
911  */
912 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
913 {
914         uint32_t status;
915         int ret;
916         struct ctdb_server_id server_id;
917
918         if (argc < 3) {
919                 usage();
920         }
921
922         server_id.pnn       = strtoul(argv[0], NULL, 0);
923         server_id.type      = strtoul(argv[1], NULL, 0);
924         server_id.server_id = strtoul(argv[2], NULL, 0);
925
926         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
927         if (ret != 0) {
928                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
929                 return ret;
930         }
931
932         if (status) {
933                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
934         } else {
935                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
936         }
937         return 0;
938 }
939
940 /*
941   get a list of all server ids that are registered on a node
942  */
943 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
944 {
945         int i, ret;
946         struct ctdb_server_id_list *server_ids;
947
948         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
949         if (ret != 0) {
950                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
951                 return ret;
952         }
953
954         for (i=0; i<server_ids->num; i++) {
955                 printf("Server id %d:%d:%d\n", 
956                         server_ids->server_ids[i].pnn, 
957                         server_ids->server_ids[i].type, 
958                         server_ids->server_ids[i].server_id); 
959         }
960
961         return -1;
962 }
963
964 /*
965   send a tcp tickle ack
966  */
967 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
968 {
969         int ret;
970         ctdb_sock_addr  src, dst;
971
972         if (argc < 2) {
973                 usage();
974         }
975
976         if (!parse_ip_port(argv[0], &src)) {
977                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
978                 return -1;
979         }
980
981         if (!parse_ip_port(argv[1], &dst)) {
982                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
983                 return -1;
984         }
985
986         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
987         if (ret==0) {
988                 return 0;
989         }
990         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
991
992         return -1;
993 }
994
995
996 /*
997   display public ip status
998  */
999 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
1000 {
1001         int i, ret;
1002         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1003         struct ctdb_all_public_ips *ips;
1004
1005         if (options.pnn == CTDB_BROADCAST_ALL) {
1006                 /* read the list of public ips from all nodes */
1007                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
1008         } else {
1009                 /* read the public ip list from this node */
1010                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
1011         }
1012         if (ret != 0) {
1013                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
1014                 talloc_free(tmp_ctx);
1015                 return ret;
1016         }
1017
1018         if (options.machinereadable){
1019                 printf(":Public IP:Node:\n");
1020         } else {
1021                 if (options.pnn == CTDB_BROADCAST_ALL) {
1022                         printf("Public IPs on ALL nodes\n");
1023                 } else {
1024                         printf("Public IPs on node %u\n", options.pnn);
1025                 }
1026         }
1027
1028         for (i=1;i<=ips->num;i++) {
1029                 if (options.machinereadable){
1030                         printf(":%s:%d:\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
1031                 } else {
1032                         printf("%s %d\n", inet_ntoa(ips->ips[ips->num-i].sin.sin_addr), ips->ips[ips->num-i].pnn);
1033                 }
1034         }
1035
1036         talloc_free(tmp_ctx);
1037         return 0;
1038 }
1039
1040 /*
1041   display pid of a ctdb daemon
1042  */
1043 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
1044 {
1045         uint32_t pid;
1046         int ret;
1047
1048         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
1049         if (ret != 0) {
1050                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
1051                 return ret;
1052         }
1053         printf("Pid:%d\n", pid);
1054
1055         return 0;
1056 }
1057
1058 /*
1059   disable a remote node
1060  */
1061 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
1062 {
1063         int ret;
1064
1065         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, NODE_FLAGS_PERMANENTLY_DISABLED, 0);
1066         if (ret != 0) {
1067                 DEBUG(DEBUG_ERR, ("Unable to disable node %u\n", options.pnn));
1068                 return ret;
1069         }
1070
1071         return 0;
1072 }
1073
1074 /*
1075   enable a disabled remote node
1076  */
1077 static int control_enable(struct ctdb_context *ctdb, int argc, const char **argv)
1078 {
1079         int ret;
1080
1081         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn, 0, NODE_FLAGS_PERMANENTLY_DISABLED);
1082         if (ret != 0) {
1083                 DEBUG(DEBUG_ERR, ("Unable to enable node %u\n", options.pnn));
1084                 return ret;
1085         }
1086
1087         return 0;
1088 }
1089
1090 /*
1091   ban a node from the cluster
1092  */
1093 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
1094 {
1095         int ret;
1096         struct ctdb_ban_info b;
1097         TDB_DATA data;
1098         uint32_t ban_time;
1099
1100         if (argc < 1) {
1101                 usage();
1102         }
1103
1104         ban_time = strtoul(argv[0], NULL, 0);
1105
1106         b.pnn = options.pnn;
1107         b.ban_time = ban_time;
1108
1109         data.dptr = (uint8_t *)&b;
1110         data.dsize = sizeof(b);
1111
1112         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_BAN_NODE, data);
1113         if (ret != 0) {
1114                 DEBUG(DEBUG_ERR,("Failed to ban node %u\n", options.pnn));
1115                 return -1;
1116         }
1117         
1118         return 0;
1119 }
1120
1121
1122 /*
1123   unban a node from the cluster
1124  */
1125 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
1126 {
1127         int ret;
1128         TDB_DATA data;
1129
1130         data.dptr = (uint8_t *)&options.pnn;
1131         data.dsize = sizeof(uint32_t);
1132
1133         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_UNBAN_NODE, data);
1134         if (ret != 0) {
1135                 DEBUG(DEBUG_ERR,("Failed to to unban node %u\n", options.pnn));
1136                 return -1;
1137         }
1138         
1139         return 0;
1140 }
1141
1142
1143 /*
1144   shutdown a daemon
1145  */
1146 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
1147 {
1148         int ret;
1149
1150         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
1151         if (ret != 0) {
1152                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
1153                 return ret;
1154         }
1155
1156         return 0;
1157 }
1158
1159 /*
1160   trigger a recovery
1161  */
1162 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
1163 {
1164         int ret;
1165
1166         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1167         if (ret != 0) {
1168                 DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
1169                 return ret;
1170         }
1171
1172         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
1173         if (ret != 0) {
1174                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
1175                 return ret;
1176         }
1177
1178         return 0;
1179 }
1180
1181
1182 /*
1183   display monitoring mode of a remote node
1184  */
1185 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
1186 {
1187         uint32_t monmode;
1188         int ret;
1189
1190         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
1191         if (ret != 0) {
1192                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
1193                 return ret;
1194         }
1195         if (!options.machinereadable){
1196                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
1197         } else {
1198                 printf(":mode:\n");
1199                 printf(":%d:\n",monmode);
1200         }
1201         return 0;
1202 }
1203
1204
1205 /*
1206   display capabilities of a remote node
1207  */
1208 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
1209 {
1210         uint32_t capabilities;
1211         int ret;
1212
1213         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
1214         if (ret != 0) {
1215                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
1216                 return ret;
1217         }
1218         
1219         if (!options.machinereadable){
1220                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
1221                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
1222         } else {
1223                 printf(":RECMASTER:LMASTER:\n");
1224                 printf(":%d:%d:\n",
1225                         !!(capabilities&CTDB_CAP_RECMASTER),
1226                         !!(capabilities&CTDB_CAP_LMASTER));
1227         }
1228         return 0;
1229 }
1230
1231 /*
1232   disable monitoring on a  node
1233  */
1234 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1235 {
1236         
1237         int ret;
1238
1239         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
1240         if (ret != 0) {
1241                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
1242                 return ret;
1243         }
1244         printf("Monitoring mode:%s\n","DISABLED");
1245
1246         return 0;
1247 }
1248
1249 /*
1250   enable monitoring on a  node
1251  */
1252 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
1253 {
1254         
1255         int ret;
1256
1257         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
1258         if (ret != 0) {
1259                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
1260                 return ret;
1261         }
1262         printf("Monitoring mode:%s\n","ACTIVE");
1263
1264         return 0;
1265 }
1266
1267 /*
1268   display remote list of keys/data for a db
1269  */
1270 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
1271 {
1272         const char *db_name;
1273         struct ctdb_db_context *ctdb_db;
1274         int ret;
1275
1276         if (argc < 1) {
1277                 usage();
1278         }
1279
1280         db_name = argv[0];
1281         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1282
1283         if (ctdb_db == NULL) {
1284                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1285                 return -1;
1286         }
1287
1288         /* traverse and dump the cluster tdb */
1289         ret = ctdb_dump_db(ctdb_db, stdout);
1290         if (ret == -1) {
1291                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
1292                 return -1;
1293         }
1294         talloc_free(ctdb_db);
1295
1296         printf("Dumped %d records\n", ret);
1297         return 0;
1298 }
1299
1300
1301 /*
1302   display a list of the databases on a remote ctdb
1303  */
1304 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
1305 {
1306         int i, ret;
1307         struct ctdb_dbid_map *dbmap=NULL;
1308
1309         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
1310         if (ret != 0) {
1311                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
1312                 return ret;
1313         }
1314
1315         printf("Number of databases:%d\n", dbmap->num);
1316         for(i=0;i<dbmap->num;i++){
1317                 const char *path;
1318                 const char *name;
1319                 bool persistent;
1320
1321                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
1322                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
1323                 persistent = dbmap->dbs[i].persistent;
1324                 printf("dbid:0x%08x name:%s path:%s %s\n", dbmap->dbs[i].dbid, name, 
1325                        path, persistent?"PERSISTENT":"");
1326         }
1327
1328         return 0;
1329 }
1330
1331 /*
1332   get the filename of the reclock file
1333  */
1334 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
1335 {
1336         int i, ret, fd;
1337         const char *reclock;
1338         struct ctdb_node_map *nodemap=NULL;
1339         char *pnnfile;
1340
1341         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
1342         if (ret != 0) {
1343                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
1344                 return ret;
1345         }
1346
1347         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1348         if (ret != 0) {
1349                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1350                 return ret;
1351         }
1352
1353
1354         pnnfile = talloc_asprintf(ctdb, "%s.pnn", reclock);
1355         CTDB_NO_MEMORY(ctdb, pnnfile);
1356
1357         fd = open(pnnfile, O_RDONLY);
1358         if (fd == -1) {
1359                 DEBUG(DEBUG_CRIT,(__location__ " Failed to open reclock pnn file %s - (%s)\n", 
1360                          pnnfile, strerror(errno)));
1361                 exit(10);
1362         }
1363
1364
1365         printf("Reclock file : %s\n", reclock);
1366         for (i=0; i<nodemap->num; i++) {
1367                 int count;
1368
1369                 count = ctdb_read_pnn_lock(fd, nodemap->nodes[i].pnn);
1370
1371                 printf("pnn:%d %-16s", nodemap->nodes[i].pnn,
1372                        inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1373                 if (count == -1) {
1374                         printf(" NOT ACTIVE\n");
1375                 } else {
1376                         printf(" ACTIVE with %d connections\n", count);
1377                 }
1378         }
1379
1380         close(fd);
1381         return 0;
1382 }
1383
1384
1385 /*
1386   check if the local node is recmaster or not
1387   it will return 1 if this node is the recmaster and 0 if it is not
1388   or if the local ctdb daemon could not be contacted
1389  */
1390 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1391 {
1392         uint32_t mypnn, recmaster;
1393         int ret;
1394
1395         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);
1396         if (mypnn == -1) {
1397                 printf("Failed to get pnn of node\n");
1398                 return 1;
1399         }
1400
1401         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1402         if (ret != 0) {
1403                 printf("Failed to get the recmaster\n");
1404                 return 1;
1405         }
1406
1407         if (recmaster != mypnn) {
1408                 printf("this node is not the recmaster\n");
1409                 return 1;
1410         }
1411
1412         printf("this node is the recmaster\n");
1413         return 0;
1414 }
1415
1416 /*
1417   ping a node
1418  */
1419 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
1420 {
1421         int ret;
1422         struct timeval tv = timeval_current();
1423         ret = ctdb_ctrl_ping(ctdb, options.pnn);
1424         if (ret == -1) {
1425                 printf("Unable to get ping response from node %u\n", options.pnn);
1426         } else {
1427                 printf("response from %u time=%.6f sec  (%d clients)\n", 
1428                        options.pnn, timeval_elapsed(&tv), ret);
1429         }
1430         return 0;
1431 }
1432
1433
1434 /*
1435   get a tunable
1436  */
1437 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
1438 {
1439         const char *name;
1440         uint32_t value;
1441         int ret;
1442
1443         if (argc < 1) {
1444                 usage();
1445         }
1446
1447         name = argv[0];
1448         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
1449         if (ret == -1) {
1450                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
1451                 return -1;
1452         }
1453
1454         printf("%-19s = %u\n", name, value);
1455         return 0;
1456 }
1457
1458 /*
1459   set a tunable
1460  */
1461 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
1462 {
1463         const char *name;
1464         uint32_t value;
1465         int ret;
1466
1467         if (argc < 2) {
1468                 usage();
1469         }
1470
1471         name = argv[0];
1472         value = strtoul(argv[1], NULL, 0);
1473
1474         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
1475         if (ret == -1) {
1476                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
1477                 return -1;
1478         }
1479         return 0;
1480 }
1481
1482 /*
1483   list all tunables
1484  */
1485 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
1486 {
1487         uint32_t count;
1488         const char **list;
1489         int ret, i;
1490
1491         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
1492         if (ret == -1) {
1493                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
1494                 return -1;
1495         }
1496
1497         for (i=0;i<count;i++) {
1498                 control_getvar(ctdb, 1, &list[i]);
1499         }
1500
1501         talloc_free(list);
1502         
1503         return 0;
1504 }
1505
1506 static struct {
1507         int32_t level;
1508         const char *description;
1509 } debug_levels[] = {
1510         {DEBUG_EMERG,   "EMERG"},
1511         {DEBUG_ALERT,   "ALERT"},
1512         {DEBUG_CRIT,    "CRIT"},
1513         {DEBUG_ERR,     "ERR"},
1514         {DEBUG_WARNING, "WARNING"},
1515         {DEBUG_NOTICE,  "NOTICE"},
1516         {DEBUG_INFO,    "INFO"},
1517         {DEBUG_DEBUG,   "DEBUG"}
1518 };
1519
1520 static const char *get_debug_by_level(int32_t level)
1521 {
1522         int i;
1523
1524         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1525                 if (debug_levels[i].level == level) {
1526                         return debug_levels[i].description;
1527                 }
1528         }
1529         return "Unknown";
1530 }
1531
1532 static int32_t get_debug_by_desc(const char *desc)
1533 {
1534         int i;
1535
1536         for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
1537                 if (!strcmp(debug_levels[i].description, desc)) {
1538                         return debug_levels[i].level;
1539                 }
1540         }
1541         return DEBUG_ERR;
1542 }
1543
1544 /*
1545   display debug level on a node
1546  */
1547 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1548 {
1549         int ret;
1550         int32_t level;
1551
1552         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
1553         if (ret != 0) {
1554                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
1555                 return ret;
1556         } else {
1557                 if (options.machinereadable){
1558                         printf(":Name:Level:\n");
1559                         printf(":%s:%d:\n",get_debug_by_level(level),level);
1560                 } else {
1561                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
1562                 }
1563         }
1564         return 0;
1565 }
1566
1567
1568 /*
1569   set debug level on a node or all nodes
1570  */
1571 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
1572 {
1573         int ret;
1574         uint32_t level;
1575
1576         if (argc < 1) {
1577                 usage();
1578         }
1579
1580         if (isalpha(argv[0][0])) { 
1581                 level = get_debug_by_desc(argv[0]);
1582         } else {
1583                 level = strtoul(argv[0], NULL, 0);
1584         }
1585
1586         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
1587         if (ret != 0) {
1588                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
1589         }
1590         return 0;
1591 }
1592
1593
1594 /*
1595   freeze a node
1596  */
1597 static int control_freeze(struct ctdb_context *ctdb, int argc, const char **argv)
1598 {
1599         int ret;
1600
1601         ret = ctdb_ctrl_freeze(ctdb, TIMELIMIT(), options.pnn);
1602         if (ret != 0) {
1603                 DEBUG(DEBUG_ERR, ("Unable to freeze node %u\n", options.pnn));
1604         }               
1605         return 0;
1606 }
1607
1608 /*
1609   thaw a node
1610  */
1611 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
1612 {
1613         int ret;
1614
1615         ret = ctdb_ctrl_thaw(ctdb, TIMELIMIT(), options.pnn);
1616         if (ret != 0) {
1617                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
1618         }               
1619         return 0;
1620 }
1621
1622
1623 /*
1624   attach to a database
1625  */
1626 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
1627 {
1628         const char *db_name;
1629         struct ctdb_db_context *ctdb_db;
1630
1631         if (argc < 1) {
1632                 usage();
1633         }
1634         db_name = argv[0];
1635
1636         ctdb_db = ctdb_attach(ctdb, db_name, false, 0);
1637         if (ctdb_db == NULL) {
1638                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
1639                 return -1;
1640         }
1641
1642         return 0;
1643 }
1644
1645 /*
1646   dump memory usage
1647  */
1648 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
1649 {
1650         TDB_DATA data;
1651         int ret;
1652         int32_t res;
1653         char *errmsg;
1654         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1655
1656         if (argc != 1) {
1657                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
1658                 return -1;
1659         }
1660
1661         data.dptr = (unsigned char *)discard_const(argv[0]);
1662         data.dsize = strlen((char *)data.dptr) + 1;
1663
1664         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
1665
1666         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
1667                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
1668         if (ret != 0 || res != 0) {
1669                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
1670                 talloc_free(tmp_ctx);
1671                 return -1;
1672         }
1673         talloc_free(tmp_ctx);
1674         return 0;
1675 }
1676
1677 /*
1678   dump memory usage
1679  */
1680 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1681 {
1682         TDB_DATA data;
1683         int ret;
1684         int32_t res;
1685         char *errmsg;
1686         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1687         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
1688                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
1689         if (ret != 0 || res != 0) {
1690                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
1691                 talloc_free(tmp_ctx);
1692                 return -1;
1693         }
1694         write(1, data.dptr, data.dsize);
1695         talloc_free(tmp_ctx);
1696         return 0;
1697 }
1698
1699 /*
1700   handler for memory dumps
1701 */
1702 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1703                              TDB_DATA data, void *private_data)
1704 {
1705         write(1, data.dptr, data.dsize);
1706         exit(0);
1707 }
1708
1709 /*
1710   dump memory usage on the recovery daemon
1711  */
1712 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
1713 {
1714         int ret;
1715         TDB_DATA data;
1716         struct rd_memdump_reply rd;
1717
1718         rd.pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1719         if (rd.pnn == -1) {
1720                 DEBUG(DEBUG_ERR, ("Failed to get pnn of local node\n"));
1721                 return -1;
1722         }
1723         rd.srvid = getpid();
1724
1725         /* register a message port for receiveing the reply so that we
1726            can receive the reply
1727         */
1728         ctdb_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
1729
1730
1731         data.dptr = (uint8_t *)&rd;
1732         data.dsize = sizeof(rd);
1733
1734         ret = ctdb_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
1735         if (ret != 0) {
1736                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
1737                 return -1;
1738         }
1739
1740         /* this loop will terminate when we have received the reply */
1741         while (1) {     
1742                 event_loop_once(ctdb->ev);
1743         }
1744
1745         return 0;
1746 }
1747
1748 /*
1749   list all nodes in the cluster
1750  */
1751 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
1752 {
1753         int i, ret;
1754         struct ctdb_node_map *nodemap=NULL;
1755
1756         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
1757         if (ret != 0) {
1758                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1759                 return ret;
1760         }
1761
1762         for(i=0;i<nodemap->num;i++){
1763                 printf("%s\n", inet_ntoa(nodemap->nodes[i].sin.sin_addr));
1764         }
1765
1766         return 0;
1767 }
1768
1769 /*
1770   reload the nodes file on the local node
1771  */
1772 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
1773 {
1774         int i, ret;
1775         int mypnn;
1776         struct ctdb_node_map *nodemap=NULL;
1777
1778         mypnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1779         if (mypnn == -1) {
1780                 DEBUG(DEBUG_ERR, ("Failed to read pnn of local node\n"));
1781                 return -1;
1782         }
1783
1784         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
1785         if (ret != 0) {
1786                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
1787                 return ret;
1788         }
1789
1790         /* reload the nodes file on all remote nodes */
1791         for (i=0;i<nodemap->num;i++) {
1792                 if (nodemap->nodes[i].pnn == mypnn) {
1793                         continue;
1794                 }
1795                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
1796                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
1797                         nodemap->nodes[i].pnn);
1798                 if (ret != 0) {
1799                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
1800                 }
1801         }
1802
1803         /* reload the nodes file on the local node */
1804         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
1805         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
1806         if (ret != 0) {
1807                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
1808         }
1809
1810         return 0;
1811 }
1812
1813
1814 static const struct {
1815         const char *name;
1816         int (*fn)(struct ctdb_context *, int, const char **);
1817         bool auto_all;
1818         const char *msg;
1819         const char *args;
1820 } ctdb_commands[] = {
1821 #ifdef CTDB_VERS
1822         { "version",         control_version,           true,  "show version of ctdb" },
1823 #endif
1824         { "status",          control_status,            true,  "show node status" },
1825         { "uptime",          control_uptime,            true,  "show node uptime" },
1826         { "ping",            control_ping,              true,  "ping all nodes" },
1827         { "getvar",          control_getvar,            true,  "get a tunable variable",               "<name>"},
1828         { "setvar",          control_setvar,            true,  "set a tunable variable",               "<name> <value>"},
1829         { "listvars",        control_listvars,          true,  "list tunable variables"},
1830         { "statistics",      control_statistics,        false, "show statistics" },
1831         { "statisticsreset", control_statistics_reset,  true,  "reset statistics"},
1832         { "ip",              control_ip,                false,  "show which public ip's that ctdb manages" },
1833         { "process-exists",  control_process_exists,    true,  "check if a process exists on a node",  "<pid>"},
1834         { "getdbmap",        control_getdbmap,          true,  "show the database map" },
1835         { "catdb",           control_catdb,             true,  "dump a database" ,                     "<dbname>"},
1836         { "getmonmode",      control_getmonmode,        true,  "show monitoring mode" },
1837         { "getcapabilities", control_getcapabilities,   true,  "show node capabilities" },
1838         { "disablemonitor",      control_disable_monmode,        true,  "set monitoring mode to DISABLE" },
1839         { "enablemonitor",      control_enable_monmode,        true,  "set monitoring mode to ACTIVE" },
1840         { "setdebug",        control_setdebug,          true,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
1841         { "getdebug",        control_getdebug,          true,  "get debug level" },
1842         { "attach",          control_attach,            true,  "attach to a database",                 "<dbname>" },
1843         { "dumpmemory",      control_dumpmemory,        true,  "dump memory map to stdout" },
1844         { "rddumpmemory",    control_rddumpmemory,      true,  "dump memory map from the recovery daemon to stdout" },
1845         { "getpid",          control_getpid,            true,  "get ctdbd process ID" },
1846         { "disable",         control_disable,           true,  "disable a nodes public IP" },
1847         { "enable",          control_enable,            true,  "enable a nodes public IP" },
1848         { "ban",             control_ban,               true,  "ban a node from the cluster",          "<bantime|0>"},
1849         { "unban",           control_unban,             true,  "unban a node from the cluster" },
1850         { "shutdown",        control_shutdown,          true,  "shutdown ctdbd" },
1851         { "recover",         control_recover,           true,  "force recovery" },
1852         { "freeze",          control_freeze,            true,  "freeze all databases" },
1853         { "thaw",            control_thaw,              true,  "thaw all databases" },
1854         { "isnotrecmaster",  control_isnotrecmaster,    false,  "check if the local node is recmaster or not" },
1855         { "killtcp",         kill_tcp,                  false, "kill a tcp connection.", "<srcip:port> <dstip:port>" },
1856         { "gratiousarp",     control_gratious_arp,      false, "send a gratious arp", "<ip> <interface>" },
1857         { "tickle",          tickle_tcp,                false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
1858         { "gettickles",      control_get_tickles,       false, "get the list of tickles registered for this ip", "<ip>" },
1859
1860         { "regsrvid",        regsrvid,                  false, "register a server id", "<pnn> <type> <id>" },
1861         { "unregsrvid",      unregsrvid,                false, "unregister a server id", "<pnn> <type> <id>" },
1862         { "chksrvid",        chksrvid,                  false, "check if a server id exists", "<pnn> <type> <id>" },
1863         { "getsrvids",       getsrvids,                 false, "get a list of all server ids"},
1864         { "vacuum",          ctdb_vacuum,               false, "vacuum the databases of empty records", "[max_records]"},
1865         { "repack",          ctdb_repack,               false, "repack all databases", "[max_freelist]"},
1866         { "listnodes",       control_listnodes,         false, "list all nodes in the cluster"},
1867         { "reloadnodes",     control_reload_nodes_file,         false, "reload the nodes file and restart the transport on all nodes"},
1868         { "getreclock",      control_getreclock,        false,  "get the path to the reclock file" },
1869         { "moveip",          control_moveip,            false, "move/failover an ip address to another node", "<ip> <node>"},
1870         { "addip",           control_addip,             true, "add a ip address to a node", "<ip/mask> <iface>"},
1871         { "delip",           control_delip,             false, "delete an ip address from a node", "<ip>"},
1872         { "eventscript",     control_eventscript,       true, "run the eventscript with the given parameters on a node", "<arguments>"},
1873 };
1874
1875 /*
1876   show usage message
1877  */
1878 static void usage(void)
1879 {
1880         int i;
1881         printf(
1882 "Usage: ctdb [options] <control>\n" \
1883 "Options:\n" \
1884 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
1885 "   -Y                 generate machinereadable output\n"
1886 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
1887         printf("Controls:\n");
1888         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1889                 printf("  %-15s %-27s  %s\n", 
1890                        ctdb_commands[i].name, 
1891                        ctdb_commands[i].args?ctdb_commands[i].args:"",
1892                        ctdb_commands[i].msg);
1893         }
1894         exit(1);
1895 }
1896
1897
1898 static void ctdb_alarm(int sig)
1899 {
1900         printf("Maximum runtime exceeded - exiting\n");
1901         _exit(0);
1902 }
1903
1904 /*
1905   main program
1906 */
1907 int main(int argc, const char *argv[])
1908 {
1909         struct ctdb_context *ctdb;
1910         char *nodestring = NULL;
1911         struct poptOption popt_options[] = {
1912                 POPT_AUTOHELP
1913                 POPT_CTDB_CMDLINE
1914                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
1915                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
1916                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
1917                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
1918                 POPT_TABLEEND
1919         };
1920         int opt;
1921         const char **extra_argv;
1922         int extra_argc = 0;
1923         int ret=-1, i;
1924         poptContext pc;
1925         struct event_context *ev;
1926         const char *control;
1927
1928         setlinebuf(stdout);
1929         
1930         /* set some defaults */
1931         options.maxruntime = 0;
1932         options.timelimit = 3;
1933         options.pnn = CTDB_CURRENT_NODE;
1934
1935         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
1936
1937         while ((opt = poptGetNextOpt(pc)) != -1) {
1938                 switch (opt) {
1939                 default:
1940                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
1941                                 poptBadOption(pc, 0), poptStrerror(opt)));
1942                         exit(1);
1943                 }
1944         }
1945
1946         /* setup the remaining options for the main program to use */
1947         extra_argv = poptGetArgs(pc);
1948         if (extra_argv) {
1949                 extra_argv++;
1950                 while (extra_argv[extra_argc]) extra_argc++;
1951         }
1952
1953         if (extra_argc < 1) {
1954                 usage();
1955         }
1956
1957         if (options.maxruntime != 0) {
1958                 signal(SIGALRM, ctdb_alarm);
1959                 alarm(options.maxruntime);
1960         }
1961
1962         /* setup the node number to contact */
1963         if (nodestring != NULL) {
1964                 if (strcmp(nodestring, "all") == 0) {
1965                         options.pnn = CTDB_BROADCAST_ALL;
1966                 } else {
1967                         options.pnn = strtoul(nodestring, NULL, 0);
1968                 }
1969         }
1970
1971         control = extra_argv[0];
1972
1973         ev = event_context_init(NULL);
1974
1975         /* initialise ctdb */
1976         ctdb = ctdb_cmdline_client(ev);
1977         if (ctdb == NULL) {
1978                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
1979                 exit(1);
1980         }
1981
1982         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
1983                 if (strcmp(control, ctdb_commands[i].name) == 0) {
1984                         int j;
1985
1986                         if (options.pnn == CTDB_CURRENT_NODE) {
1987                                 int pnn;
1988                                 pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), options.pnn);         
1989                                 if (pnn == -1) {
1990                                         return -1;
1991                                 }
1992                                 options.pnn = pnn;
1993                         }
1994
1995                         if (ctdb_commands[i].auto_all && 
1996                             options.pnn == CTDB_BROADCAST_ALL) {
1997                                 uint32_t *nodes;
1998                                 uint32_t num_nodes;
1999                                 ret = 0;
2000
2001                                 nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
2002                                 CTDB_NO_MEMORY(ctdb, nodes);
2003         
2004                                 for (j=0;j<num_nodes;j++) {
2005                                         options.pnn = nodes[j];
2006                                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2007                                 }
2008                                 talloc_free(nodes);
2009                         } else {
2010                                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
2011                         }
2012                         break;
2013                 }
2014         }
2015
2016         if (i == ARRAY_SIZE(ctdb_commands)) {
2017                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
2018                 exit(1);
2019         }
2020
2021         return ret;
2022 }