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