eventscript: use -ENOEXEC for disabled status value
[metze/ctdb/wip.git] / include / ctdb_private.h
1 /* 
2    ctdb database library
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef _CTDB_PRIVATE_H
21 #define _CTDB_PRIVATE_H
22
23 #include "ctdb.h"
24 #include <sys/socket.h>
25
26 /* location of daemon socket */
27 #define CTDB_PATH       "/tmp/ctdb.socket"
28
29 /* default ctdb port number */
30 #define CTDB_PORT 4379
31
32 /* we must align packets to ensure ctdb works on all architectures (eg. sparc) */
33 #define CTDB_DS_ALIGNMENT 8
34
35
36 #define CTDB_NULL_FUNC      0xFF000001
37 #define CTDB_FETCH_FUNC     0xFF000002
38
39
40 /*
41   recovery daemon memdump reply address
42  */
43 struct rd_memdump_reply {
44         uint32_t pnn;
45         uint64_t srvid;
46 };
47
48 /*
49   description for a TAKEOVER_RUN message reply address
50  */
51 struct takeover_run_reply {
52         uint32_t pnn;
53         uint64_t srvid;
54 };
55
56 /*
57  * pid of the ctdbd daemon
58  */
59 extern pid_t ctdbd_pid;
60
61 /*
62   a tcp connection description
63  */
64 struct ctdb_tcp_connection {
65         ctdb_sock_addr src_addr;
66         ctdb_sock_addr dst_addr;
67 };
68
69 /* the wire representation for a tcp tickle array */
70 struct ctdb_tcp_wire_array {
71         uint32_t num;
72         struct ctdb_tcp_connection connections[1];
73 };      
74
75 /* the list of tcp tickles used by get/set tcp tickle list */
76 struct ctdb_control_tcp_tickle_list {
77         ctdb_sock_addr addr;
78         struct ctdb_tcp_wire_array tickles;
79 };
80
81 /*
82   array of tcp connections
83  */
84 struct ctdb_tcp_array {
85         uint32_t num;
86         struct ctdb_tcp_connection *connections;
87 };      
88
89
90 /* all tunable variables go in here */
91 struct ctdb_tunable {
92         uint32_t max_redirect_count;
93         uint32_t seqnum_interval; /* unit is ms */
94         uint32_t control_timeout;
95         uint32_t traverse_timeout;
96         uint32_t keepalive_interval;
97         uint32_t keepalive_limit;
98         uint32_t max_lacount;
99         uint32_t recover_timeout;
100         uint32_t recover_interval;
101         uint32_t election_timeout;
102         uint32_t takeover_timeout;
103         uint32_t monitor_interval;
104         uint32_t tickle_update_interval;
105         uint32_t script_timeout;
106         uint32_t script_ban_count; /* ban after this many consec timeouts*/
107         uint32_t script_unhealthy_on_timeout; /* don't ban on timeout; set node unhealthy */
108         uint32_t recovery_grace_period;
109         uint32_t recovery_ban_period;
110         uint32_t database_hash_size;
111         uint32_t database_max_dead;
112         uint32_t rerecovery_timeout;
113         uint32_t enable_bans;
114         uint32_t deterministic_public_ips;
115         uint32_t disable_when_unhealthy;
116         uint32_t reclock_ping_period;
117         uint32_t no_ip_failback;
118         uint32_t verbose_memory_names;
119         uint32_t recd_ping_timeout;
120         uint32_t recd_ping_failcount;
121         uint32_t log_latency_ms;
122         uint32_t reclock_latency_ms;
123         uint32_t recovery_drop_all_ips;
124         uint32_t verify_recovery_lock;
125         uint32_t vacuum_default_interval;
126         uint32_t vacuum_max_run_time;
127         uint32_t repack_limit;
128         uint32_t vacuum_limit;
129         uint32_t vacuum_min_interval;
130         uint32_t vacuum_max_interval;
131         uint32_t max_queue_depth_drop_msg;
132         uint32_t use_status_events_for_monitoring;
133 };
134
135 /*
136   an installed ctdb remote call
137 */
138 struct ctdb_registered_call {
139         struct ctdb_registered_call *next, *prev;
140         uint32_t id;
141         ctdb_fn_t fn;
142 };
143
144 /*
145   this address structure might need to be generalised later for some
146   transports
147 */
148 struct ctdb_address {
149         const char *address;
150         int port;
151 };
152
153 /*
154   check that a pnn is valid
155  */
156 #define ctdb_validate_pnn(ctdb, pnn) (((uint32_t)(pnn)) < (ctdb)->num_nodes)
157
158
159 /* called from the queue code when a packet comes in. Called with data==NULL
160    on error */
161 typedef void (*ctdb_queue_cb_fn_t)(uint8_t *data, size_t length,
162                                    void *private_data);
163
164 /* used for callbacks in ctdb_control requests */
165 typedef void (*ctdb_control_callback_fn_t)(struct ctdb_context *,
166                                            int32_t status, TDB_DATA data, 
167                                            const char *errormsg,
168                                            void *private_data);
169 /*
170   structure describing a connected client in the daemon
171  */
172 struct ctdb_client {
173         struct ctdb_context *ctdb;
174         int fd;
175         struct ctdb_queue *queue;
176         uint32_t client_id;
177         pid_t pid;
178         struct ctdb_tcp_list *tcp_list;
179         uint32_t db_id;
180         uint32_t num_persistent_updates;
181         struct ctdb_client_notify_list *notify;
182 };
183
184
185 /* state associated with a public ip address */
186 struct ctdb_vnn {
187         struct ctdb_vnn *prev, *next;
188
189         const char *iface;
190         ctdb_sock_addr public_address;
191         uint8_t public_netmask_bits;
192
193         /* the node number that is serving this public address, if any. 
194            If no node serves this ip it is set to -1 */
195         int32_t pnn;
196
197         /* List of clients to tickle for this public address */
198         struct ctdb_tcp_array *tcp_array;
199
200         /* whether we need to update the other nodes with changes to our list
201            of connected clients */
202         bool tcp_update_needed;
203
204         /* a context to hang sending gratious arp events off */
205         TALLOC_CTX *takeover_ctx;
206
207         struct ctdb_kill_tcp *killtcp;
208 };
209
210 /*
211   state associated with one node
212 */
213 struct ctdb_node {
214         struct ctdb_context *ctdb;
215         struct ctdb_address address;
216         const char *name; /* for debug messages */
217         void *private_data; /* private to transport */
218         uint32_t pnn;
219 #define NODE_FLAGS_DISCONNECTED         0x00000001 /* node isn't connected */
220 #define NODE_FLAGS_UNHEALTHY            0x00000002 /* monitoring says node is unhealthy */
221 #define NODE_FLAGS_PERMANENTLY_DISABLED 0x00000004 /* administrator has disabled node */
222 #define NODE_FLAGS_BANNED               0x00000008 /* recovery daemon has banned the node */
223 #define NODE_FLAGS_DELETED              0x00000010 /* this node has been deleted */
224 #define NODE_FLAGS_STOPPED              0x00000020 /* this node has been stopped */
225 #define NODE_FLAGS_DISABLED             (NODE_FLAGS_UNHEALTHY|NODE_FLAGS_PERMANENTLY_DISABLED)
226 #define NODE_FLAGS_INACTIVE             (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED|NODE_FLAGS_BANNED|NODE_FLAGS_STOPPED)
227         uint32_t flags;
228
229         /* used by the dead node monitoring */
230         uint32_t dead_count;
231         uint32_t rx_cnt;
232         uint32_t tx_cnt;
233
234         /* used to track node capabilities, is only valid/tracked inside the
235            recovery daemon.
236         */
237         uint32_t capabilities;
238
239         /* a list of controls pending to this node, so we can time them out quickly
240            if the node becomes disconnected */
241         struct daemon_control_state *pending_controls;
242
243         /* used by the recovery daemon when distributing ip addresses 
244            across the nodes.  it needs to know which public ip's can be handled
245            by each node.
246         */
247         struct ctdb_all_public_ips *public_ips;
248         /* used by the recovery dameon to track when a node should be banned */
249         struct ctdb_banning_state *ban_state; 
250 };
251
252 /*
253   transport specific methods
254 */
255 struct ctdb_methods {
256         int (*initialise)(struct ctdb_context *); /* initialise transport structures */ 
257         int (*start)(struct ctdb_context *); /* start the transport */
258         int (*add_node)(struct ctdb_node *); /* setup a new node */     
259         int (*connect_node)(struct ctdb_node *); /* connect to node */
260         int (*queue_pkt)(struct ctdb_node *, uint8_t *data, uint32_t length);
261         void *(*allocate_pkt)(TALLOC_CTX *mem_ctx, size_t );
262         void (*shutdown)(struct ctdb_context *); /* shutdown transport */
263         void (*restart)(struct ctdb_node *); /* stop and restart the connection */
264 };
265
266 /*
267   transport calls up to the ctdb layer
268 */
269 struct ctdb_upcalls {
270         /* recv_pkt is called when a packet comes in */
271         void (*recv_pkt)(struct ctdb_context *, uint8_t *data, uint32_t length);
272
273         /* node_dead is called when an attempt to send to a node fails */
274         void (*node_dead)(struct ctdb_node *);
275
276         /* node_connected is called when a connection to a node is established */
277         void (*node_connected)(struct ctdb_node *);
278 };
279
280 /* list of message handlers - needs to be changed to a more efficient data
281    structure so we can find a message handler given a srvid quickly */
282 struct ctdb_message_list {
283         struct ctdb_context *ctdb;
284         struct ctdb_message_list *next, *prev;
285         uint64_t srvid;
286         ctdb_message_fn_t message_handler;
287         void *message_private;
288 };
289
290 /* additional data required for the daemon mode */
291 struct ctdb_daemon_data {
292         int sd;
293         char *name;
294         struct ctdb_queue *queue;
295 };
296
297 /*
298   ctdb status information
299  */
300 struct ctdb_statistics {
301         uint32_t num_clients;
302         uint32_t frozen;
303         uint32_t recovering;
304         uint32_t client_packets_sent;
305         uint32_t client_packets_recv;
306         uint32_t node_packets_sent;
307         uint32_t node_packets_recv;
308         uint32_t keepalive_packets_sent;
309         uint32_t keepalive_packets_recv;
310         struct {
311                 uint32_t req_call;
312                 uint32_t reply_call;
313                 uint32_t req_dmaster;
314                 uint32_t reply_dmaster;
315                 uint32_t reply_error;
316                 uint32_t req_message;
317                 uint32_t req_control;
318                 uint32_t reply_control;
319         } node;
320         struct {
321                 uint32_t req_call;
322                 uint32_t req_message;
323                 uint32_t req_control;
324         } client;
325         struct {
326                 uint32_t call;
327                 uint32_t control;
328                 uint32_t traverse;
329         } timeouts;
330         struct {
331                 double ctdbd;
332                 double recd;
333         } reclock;
334         uint32_t total_calls;
335         uint32_t pending_calls;
336         uint32_t lockwait_calls;
337         uint32_t pending_lockwait_calls;
338         uint32_t childwrite_calls;
339         uint32_t pending_childwrite_calls;
340         uint32_t memory_used;
341         uint32_t __last_counter; /* hack for control_statistics_all */
342         uint32_t max_hop_count;
343         double max_call_latency;
344         double max_lockwait_latency;
345         double max_childwrite_latency;
346 };
347
348
349 #define INVALID_GENERATION 1
350 /* table that contains the mapping between a hash value and lmaster
351  */
352 struct ctdb_vnn_map {
353         uint32_t generation;
354         uint32_t size;
355         uint32_t *map;
356 };
357
358 /* 
359    a wire representation of the vnn map
360  */
361 struct ctdb_vnn_map_wire {
362         uint32_t generation;
363         uint32_t size;
364         uint32_t map[1];
365 };
366
367 /* a structure that contains the elements required for the write record
368    control
369 */
370 struct ctdb_write_record {
371         uint32_t dbid;
372         uint32_t keylen;
373         uint32_t datalen;
374         unsigned char blob[1];
375 };
376
377 enum ctdb_freeze_mode {CTDB_FREEZE_NONE, CTDB_FREEZE_PENDING, CTDB_FREEZE_FROZEN};
378
379 #define CTDB_MONITORING_ACTIVE          0
380 #define CTDB_MONITORING_DISABLED        1
381
382 /* The different capabilities of the ctdb daemon. */
383 #define CTDB_CAP_RECMASTER              0x00000001
384 #define CTDB_CAP_LMASTER                0x00000002
385 /* This capability is set if CTDB_LVS_PUBLIC_IP is set */
386 #define CTDB_CAP_LVS                    0x00000004
387 /* This capability is set if NATGW is enabled */
388 #define CTDB_CAP_NATGW                  0x00000008
389
390 #define NUM_DB_PRIORITIES 3
391 /* main state of the ctdb daemon */
392 struct ctdb_context {
393         struct event_context *ev;
394         struct timeval ctdbd_start_time;
395         struct timeval last_recovery_started;
396         struct timeval last_recovery_finished;
397         uint32_t recovery_mode;
398         TALLOC_CTX *tickle_update_context;
399         TALLOC_CTX *keepalive_ctx;
400         struct ctdb_tunable tunable;
401         enum ctdb_freeze_mode freeze_mode[NUM_DB_PRIORITIES+1];
402         struct ctdb_freeze_handle *freeze_handles[NUM_DB_PRIORITIES+1];
403         bool freeze_transaction_started;
404         uint32_t freeze_transaction_id;
405         struct ctdb_address address;
406         const char *name;
407         const char *db_directory;
408         const char *db_directory_persistent;
409         const char *transport;
410         char *recovery_lock_file;
411         int recovery_lock_fd;
412         uint32_t pnn; /* our own pnn */
413         uint32_t num_nodes;
414         uint32_t num_connected;
415         unsigned flags;
416         uint32_t capabilities;
417         struct idr_context *idr;
418         uint16_t idr_cnt;
419         struct ctdb_node **nodes; /* array of nodes in the cluster - indexed by vnn */
420         struct ctdb_vnn *vnn; /* list of public ip addresses and interfaces */
421         struct ctdb_vnn *single_ip_vnn; /* a structure for the single ip */
422         char *err_msg;
423         const struct ctdb_methods *methods; /* transport methods */
424         const struct ctdb_upcalls *upcalls; /* transport upcalls */
425         void *private_data; /* private to transport */
426         struct ctdb_db_context *db_list;
427         struct ctdb_message_list *message_list;
428         struct ctdb_daemon_data daemon;
429         struct ctdb_statistics statistics;
430         struct ctdb_vnn_map *vnn_map;
431         uint32_t num_clients;
432         uint32_t recovery_master;
433         struct ctdb_call_state *pending_calls;
434         struct ctdb_client_ip *client_ip_list;
435         bool do_setsched;
436         bool do_checkpublicip;
437         void *saved_scheduler_param;
438         struct _trbt_tree_t *server_ids;        
439         const char *event_script_dir;
440         const char *notification_script;
441         const char *default_public_interface;
442         pid_t ctdbd_pid;
443         pid_t recoverd_pid;
444         pid_t syslogd_pid;
445         bool done_startup;
446         const char *node_ip;
447         struct ctdb_monitor_state *monitor;
448         struct ctdb_log_state *log;
449         int start_as_disabled;
450         int start_as_stopped;
451         uint32_t event_script_timeouts; /* counting how many consecutive times an eventscript has timedout */
452         uint32_t *recd_ping_count;
453         TALLOC_CTX *release_ips_ctx; /* a context used to automatically drop all IPs if we fail to recover the node */
454
455         TALLOC_CTX *monitor_event_script_ctx;
456         TALLOC_CTX *other_event_script_ctx;
457
458         struct ctdb_monitor_script_status_ctx *current_monitor_status_ctx;
459         struct ctdb_monitoring_wire *last_status;
460
461         TALLOC_CTX *banning_ctx;
462
463         /* mapping from pid to ctdb_client * */
464         struct ctdb_client_pid_list *client_pids;
465 };
466
467 struct ctdb_db_context {
468         struct ctdb_db_context *next, *prev;
469         struct ctdb_context *ctdb;
470         uint32_t db_id;
471         uint32_t priority;
472         bool persistent;
473         const char *db_name;
474         const char *db_path;
475         struct tdb_wrap *ltdb;
476         struct ctdb_registered_call *calls; /* list of registered calls */
477         uint32_t seqnum;
478         struct timed_event *te;
479         struct ctdb_traverse_local_handle *traverse;
480         bool transaction_active;
481         struct ctdb_vacuum_handle *vacuum_handle;
482 };
483
484
485 #define CTDB_NO_MEMORY(ctdb, p) do { if (!(p)) { \
486           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
487           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
488           return -1; }} while (0)
489
490 #define CTDB_NO_MEMORY_VOID(ctdb, p) do { if (!(p)) { \
491           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
492           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
493           return; }} while (0)
494
495 #define CTDB_NO_MEMORY_NULL(ctdb, p) do { if (!(p)) { \
496           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
497           ctdb_set_error(ctdb, "Out of memory at %s:%d", __FILE__, __LINE__); \
498           return NULL; }} while (0)
499
500 #define CTDB_NO_MEMORY_FATAL(ctdb, p) do { if (!(p)) { \
501           DEBUG(0,("Out of memory for %s at %s\n", #p, __location__)); \
502           ctdb_fatal(ctdb, "Out of memory in " __location__ ); \
503           }} while (0)
504
505 /*
506   the extended header for records in the ltdb
507 */
508 struct ctdb_ltdb_header {
509         uint64_t rsn;
510         uint32_t dmaster;
511         uint32_t laccessor;
512         uint32_t lacount;
513 };
514
515 enum ctdb_controls {CTDB_CONTROL_PROCESS_EXISTS          = 0, 
516                     CTDB_CONTROL_STATISTICS              = 1, 
517                     /* #2 removed */
518                     CTDB_CONTROL_PING                    = 3,
519                     CTDB_CONTROL_GETDBPATH               = 4,
520                     CTDB_CONTROL_GETVNNMAP               = 5,
521                     CTDB_CONTROL_SETVNNMAP               = 6,
522                     CTDB_CONTROL_GET_DEBUG               = 7,
523                     CTDB_CONTROL_SET_DEBUG               = 8,
524                     CTDB_CONTROL_GET_DBMAP               = 9,
525                     CTDB_CONTROL_GET_NODEMAPv4           = 10, /* obsolete */
526                     CTDB_CONTROL_SET_DMASTER             = 11,
527                     /* #12 removed */
528                     CTDB_CONTROL_PULL_DB                 = 13,
529                     CTDB_CONTROL_PUSH_DB                 = 14,
530                     CTDB_CONTROL_GET_RECMODE             = 15,
531                     CTDB_CONTROL_SET_RECMODE             = 16,
532                     CTDB_CONTROL_STATISTICS_RESET        = 17,
533                     CTDB_CONTROL_DB_ATTACH               = 18,
534                     CTDB_CONTROL_SET_CALL                = 19,
535                     CTDB_CONTROL_TRAVERSE_START          = 20,
536                     CTDB_CONTROL_TRAVERSE_ALL            = 21,
537                     CTDB_CONTROL_TRAVERSE_DATA           = 22,
538                     CTDB_CONTROL_REGISTER_SRVID          = 23,
539                     CTDB_CONTROL_DEREGISTER_SRVID        = 24,
540                     CTDB_CONTROL_GET_DBNAME              = 25,
541                     CTDB_CONTROL_ENABLE_SEQNUM           = 26,
542                     CTDB_CONTROL_UPDATE_SEQNUM           = 27,
543                     /* #28 removed */
544                     CTDB_CONTROL_DUMP_MEMORY             = 29,
545                     CTDB_CONTROL_GET_PID                 = 30,
546                     CTDB_CONTROL_GET_RECMASTER           = 31,
547                     CTDB_CONTROL_SET_RECMASTER           = 32,
548                     CTDB_CONTROL_FREEZE                  = 33,
549                     CTDB_CONTROL_THAW                    = 34,
550                     CTDB_CONTROL_GET_PNN                 = 35,
551                     CTDB_CONTROL_SHUTDOWN                = 36,
552                     CTDB_CONTROL_GET_MONMODE             = 37,
553                     /* #38 removed */
554                     /* #39 removed */
555                     /* #40 removed */
556                     /* #41 removed */
557                     CTDB_CONTROL_TAKEOVER_IPv4           = 42, /* obsolete */
558                     CTDB_CONTROL_RELEASE_IPv4            = 43, /* obsolete */
559                     CTDB_CONTROL_TCP_CLIENT              = 44,
560                     CTDB_CONTROL_TCP_ADD                 = 45,
561                     CTDB_CONTROL_TCP_REMOVE              = 46,
562                     CTDB_CONTROL_STARTUP                 = 47,
563                     CTDB_CONTROL_SET_TUNABLE             = 48,
564                     CTDB_CONTROL_GET_TUNABLE             = 49,
565                     CTDB_CONTROL_LIST_TUNABLES           = 50,
566                     CTDB_CONTROL_GET_PUBLIC_IPSv4        = 51, /* obsolete */
567                     CTDB_CONTROL_MODIFY_FLAGS            = 52,
568                     CTDB_CONTROL_GET_ALL_TUNABLES        = 53,
569                     CTDB_CONTROL_KILL_TCP                = 54,
570                     CTDB_CONTROL_GET_TCP_TICKLE_LIST     = 55,
571                     CTDB_CONTROL_SET_TCP_TICKLE_LIST     = 56,
572                     CTDB_CONTROL_REGISTER_SERVER_ID      = 57,
573                     CTDB_CONTROL_UNREGISTER_SERVER_ID    = 58,
574                     CTDB_CONTROL_CHECK_SERVER_ID         = 59,
575                     CTDB_CONTROL_GET_SERVER_ID_LIST      = 60,
576                     CTDB_CONTROL_DB_ATTACH_PERSISTENT    = 61,
577                     CTDB_CONTROL_PERSISTENT_STORE        = 62,
578                     CTDB_CONTROL_UPDATE_RECORD           = 63,
579                     CTDB_CONTROL_SEND_GRATIOUS_ARP       = 64,
580                     CTDB_CONTROL_TRANSACTION_START       = 65,
581                     CTDB_CONTROL_TRANSACTION_COMMIT      = 66,
582                     CTDB_CONTROL_WIPE_DATABASE           = 67,
583                     /* #68 removed */
584                     CTDB_CONTROL_UPTIME                  = 69,
585                     CTDB_CONTROL_START_RECOVERY          = 70,
586                     CTDB_CONTROL_END_RECOVERY            = 71,
587                     CTDB_CONTROL_RELOAD_NODES_FILE       = 72,
588                     /* #73 removed */
589                     CTDB_CONTROL_TRY_DELETE_RECORDS      = 74,
590                     CTDB_CONTROL_ENABLE_MONITOR          = 75,
591                     CTDB_CONTROL_DISABLE_MONITOR         = 76,
592                     CTDB_CONTROL_ADD_PUBLIC_IP           = 77,
593                     CTDB_CONTROL_DEL_PUBLIC_IP           = 78,
594                     CTDB_CONTROL_RUN_EVENTSCRIPTS        = 79,
595                     CTDB_CONTROL_GET_CAPABILITIES        = 80,
596                     CTDB_CONTROL_START_PERSISTENT_UPDATE = 81,
597                     CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE= 82,
598                     CTDB_CONTROL_TRANS2_COMMIT           = 83,
599                     CTDB_CONTROL_TRANS2_FINISHED         = 84,
600                     CTDB_CONTROL_TRANS2_ERROR            = 85,
601                     CTDB_CONTROL_TRANS2_COMMIT_RETRY     = 86,
602                     CTDB_CONTROL_RECD_PING               = 87,
603                     CTDB_CONTROL_RELEASE_IP              = 88,
604                     CTDB_CONTROL_TAKEOVER_IP             = 89,
605                     CTDB_CONTROL_GET_PUBLIC_IPS          = 90,
606                     CTDB_CONTROL_GET_NODEMAP             = 91,
607                     CTDB_CONTROL_EVENT_SCRIPT_INIT       = 92,
608                     CTDB_CONTROL_EVENT_SCRIPT_START      = 93,
609                     CTDB_CONTROL_EVENT_SCRIPT_STOP       = 94,
610                     CTDB_CONTROL_EVENT_SCRIPT_FINISHED   = 95,
611                     CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS = 96,
612                     CTDB_CONTROL_TRAVERSE_KILL           = 97,
613                     CTDB_CONTROL_RECD_RECLOCK_LATENCY    = 98,
614                     CTDB_CONTROL_GET_RECLOCK_FILE        = 99,
615                     CTDB_CONTROL_SET_RECLOCK_FILE        = 100,
616                     CTDB_CONTROL_STOP_NODE               = 101,
617                     CTDB_CONTROL_CONTINUE_NODE           = 102,
618                     CTDB_CONTROL_SET_NATGWSTATE          = 103,
619                     CTDB_CONTROL_SET_LMASTERROLE         = 104,
620                     CTDB_CONTROL_SET_RECMASTERROLE       = 105,
621                     CTDB_CONTROL_ENABLE_SCRIPT           = 107,
622                     CTDB_CONTROL_DISABLE_SCRIPT          = 108,
623                     CTDB_CONTROL_SET_BAN_STATE           = 109,
624                     CTDB_CONTROL_GET_BAN_STATE           = 110,
625                     CTDB_CONTROL_SET_DB_PRIORITY         = 111,
626                     CTDB_CONTROL_GET_DB_PRIORITY         = 112,
627                     CTDB_CONTROL_TRANSACTION_CANCEL      = 113,
628                     CTDB_CONTROL_REGISTER_NOTIFY         = 114,
629                     CTDB_CONTROL_DEREGISTER_NOTIFY       = 115,
630                     CTDB_CONTROL_TRANS2_ACTIVE           = 116,
631                     CTDB_CONTROL_GET_LOG                 = 117,
632                     CTDB_CONTROL_CLEAR_LOG               = 118,
633 };      
634
635 /*
636   structure passed in set_call control
637  */
638 struct ctdb_control_set_call {
639         uint32_t db_id;
640         ctdb_fn_t fn;
641         uint32_t id;
642 };
643
644 /*
645   struct for kill_tcp control
646  */
647 struct ctdb_control_killtcp {
648         ctdb_sock_addr src_addr;
649         ctdb_sock_addr dst_addr;
650 };
651
652 /*
653   struct holding a ctdb_sock_addr and an interface name,
654   used to add/remove public addresses
655  */
656 struct ctdb_control_ip_iface {
657         ctdb_sock_addr addr;
658         uint32_t mask;
659         uint32_t len;
660         char iface[1];
661 };
662
663 /*
664   struct holding a ctdb_sock_addr and an interface name,
665   used for send_gratious_arp
666  */
667 struct ctdb_control_gratious_arp {
668         ctdb_sock_addr addr;
669         uint32_t mask;
670         uint32_t len;
671         char iface[1];
672 };
673
674 /*
675   struct for tcp_add and tcp_remove controls
676  */
677 struct ctdb_control_tcp_vnn {
678         ctdb_sock_addr src;
679         ctdb_sock_addr dest;
680 };
681
682 /*
683   persistent store control - update this record on all other nodes
684  */
685 struct ctdb_control_persistent_store {
686         uint32_t db_id;
687         uint32_t len;
688         uint8_t  data[1];
689 };
690
691 /*
692   structure used for CTDB_SRVID_NODE_FLAGS_CHANGED
693  */
694 struct ctdb_node_flag_change {
695         uint32_t pnn;
696         uint32_t new_flags;
697         uint32_t old_flags;
698 };
699
700 /*
701   struct for admin setting a ban
702  */
703 struct ctdb_ban_info {
704         uint32_t pnn;
705         uint32_t ban_time;
706 };
707
708 enum call_state {CTDB_CALL_WAIT, CTDB_CALL_DONE, CTDB_CALL_ERROR};
709
710 #define CTDB_LMASTER_ANY        0xffffffff
711
712 /*
713   state of a in-progress ctdb call
714 */
715 struct ctdb_call_state {
716         struct ctdb_call_state *next, *prev;
717         enum call_state state;
718         uint32_t reqid;
719         struct ctdb_req_call *c;
720         struct ctdb_db_context *ctdb_db;
721         const char *errmsg;
722         struct ctdb_call *call;
723         uint32_t generation;
724         struct {
725                 void (*fn)(struct ctdb_call_state *);
726                 void *private_data;
727         } async;
728 };
729
730
731 /* used for fetch_lock */
732 struct ctdb_fetch_handle {
733         struct ctdb_db_context *ctdb_db;
734         TDB_DATA key;
735         TDB_DATA *data;
736         struct ctdb_ltdb_header header;
737 };
738
739 /*
740   operation IDs
741 */
742 enum ctdb_operation {
743         CTDB_REQ_CALL           = 0,
744         CTDB_REPLY_CALL         = 1,
745         CTDB_REQ_DMASTER        = 2,
746         CTDB_REPLY_DMASTER      = 3,
747         CTDB_REPLY_ERROR        = 4,
748         CTDB_REQ_MESSAGE        = 5,
749         /* #6 removed */
750         CTDB_REQ_CONTROL        = 7,
751         CTDB_REPLY_CONTROL      = 8,
752         CTDB_REQ_KEEPALIVE      = 9,
753 };
754
755 #define CTDB_MAGIC 0x43544442 /* CTDB */
756 #define CTDB_VERSION 1
757
758 /*
759   packet structures
760 */
761 struct ctdb_req_header {
762         uint32_t length;
763         uint32_t ctdb_magic;
764         uint32_t ctdb_version;
765         uint32_t generation;
766         uint32_t operation;
767         uint32_t destnode;
768         uint32_t srcnode;
769         uint32_t reqid;
770 };
771
772 struct ctdb_req_call {
773         struct ctdb_req_header hdr;
774         uint32_t flags;
775         uint32_t db_id;
776         uint32_t callid;
777         uint32_t hopcount;
778         uint32_t keylen;
779         uint32_t calldatalen;
780         uint8_t data[1]; /* key[] followed by calldata[] */
781 };
782
783 struct ctdb_reply_call {
784         struct ctdb_req_header hdr;
785         uint32_t status;
786         uint32_t datalen;
787         uint8_t  data[1];
788 };
789
790 struct ctdb_reply_error {
791         struct ctdb_req_header hdr;
792         uint32_t status;
793         uint32_t msglen;
794         uint8_t  msg[1];
795 };
796
797 struct ctdb_req_dmaster {
798         struct ctdb_req_header hdr;
799         uint32_t db_id;
800         uint64_t rsn;
801         uint32_t dmaster;
802         uint32_t keylen;
803         uint32_t datalen;
804         uint8_t  data[1];
805 };
806
807 struct ctdb_reply_dmaster {
808         struct ctdb_req_header hdr;
809         uint32_t db_id;
810         uint64_t rsn;
811         uint32_t keylen;
812         uint32_t datalen;
813         uint8_t  data[1];
814 };
815
816 struct ctdb_req_message {
817         struct ctdb_req_header hdr;
818         uint64_t srvid;
819         uint32_t datalen;
820         uint8_t data[1];
821 };
822
823 struct ctdb_req_getdbpath {
824         struct ctdb_req_header hdr;
825         uint32_t db_id;
826 };
827
828 struct ctdb_reply_getdbpath {
829         struct ctdb_req_header hdr;
830         uint32_t datalen;
831         uint8_t data[1];
832 };
833
834 struct ctdb_req_control {
835         struct ctdb_req_header hdr;
836         uint32_t opcode;
837         uint64_t srvid;
838         uint32_t client_id;
839 #define CTDB_CTRL_FLAG_NOREPLY   1
840         uint32_t flags;
841         uint32_t datalen;
842         uint8_t data[1];
843 };
844
845 struct ctdb_reply_control {
846         struct ctdb_req_header hdr;
847         int32_t  status;
848         uint32_t datalen;
849         uint32_t errorlen;
850         uint8_t data[1];
851 };
852
853 struct ctdb_req_keepalive {
854         struct ctdb_req_header hdr;
855 };
856
857
858 /* types of failures possible from TRANS2_COMMIT */
859 enum ctdb_trans2_commit_error {
860         CTDB_TRANS2_COMMIT_SUCCESS=0, /* all nodes committed successfully */
861         CTDB_TRANS2_COMMIT_TIMEOUT=1, /* at least one node timed out */
862         CTDB_TRANS2_COMMIT_ALLFAIL=2, /* all nodes failed the commit */
863         CTDB_TRANS2_COMMIT_SOMEFAIL=3 /* some nodes failed the commit, some allowed it */
864 };
865
866 /* different calls to event scripts. */
867 enum ctdb_eventscript_call {
868         CTDB_EVENT_STARTUP,             /* CTDB starting up: no args. */
869         CTDB_EVENT_START_RECOVERY,      /* CTDB recovery starting: no args. */
870         CTDB_EVENT_RECOVERED,           /* CTDB recovery finished: no args. */
871         CTDB_EVENT_TAKE_IP,             /* IP taken: interface, IP address, netmask bits. */
872         CTDB_EVENT_RELEASE_IP,          /* IP released: interface, IP address, netmask bits. */
873         CTDB_EVENT_STOPPED,             /* This node is stopped: no args. */
874         CTDB_EVENT_MONITOR,             /* Please check if service is healthy: no args. */
875         CTDB_EVENT_STATUS,              /* Report service status: no args. */
876         CTDB_EVENT_SHUTDOWN,            /* CTDB shutting down: no args. */
877         CTDB_EVENT_RELOAD               /* magic */
878 };
879
880 /* internal prototypes */
881 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
882 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg);
883 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2);
884 int ctdb_parse_address(struct ctdb_context *ctdb,
885                        TALLOC_CTX *mem_ctx, const char *str,
886                        struct ctdb_address *address);
887 bool ctdb_same_ip(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
888 bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2);
889 uint32_t ctdb_hash(const TDB_DATA *key);
890 uint32_t ctdb_hash_string(const char *str);
891 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
892 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
893 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
894 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
895 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
896 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
897
898 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key);
899 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
900                     TDB_DATA key, struct ctdb_ltdb_header *header, 
901                     TALLOC_CTX *mem_ctx, TDB_DATA *data);
902 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
903                     struct ctdb_ltdb_header *header, TDB_DATA data);
904 int32_t ctdb_control_start_persistent_update(struct ctdb_context *ctdb, 
905                         struct ctdb_req_control *c,
906                         TDB_DATA recdata);
907 int32_t ctdb_control_cancel_persistent_update(struct ctdb_context *ctdb, 
908                         struct ctdb_req_control *c,
909                         TDB_DATA recdata);
910 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
911 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode);
912 int ctdb_ltdb_lock_requeue(struct ctdb_db_context *ctdb_db, 
913                            TDB_DATA key, struct ctdb_req_header *hdr,
914                            void (*recv_pkt)(void *, struct ctdb_req_header *),
915                            void *recv_context, bool ignore_generation);
916 int ctdb_ltdb_lock_fetch_requeue(struct ctdb_db_context *ctdb_db, 
917                                  TDB_DATA key, struct ctdb_ltdb_header *header, 
918                                  struct ctdb_req_header *hdr, TDB_DATA *data,
919                                  void (*recv_pkt)(void *, struct ctdb_req_header *),
920                                  void *recv_context, bool ignore_generation);
921 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *);
922
923 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
924                                              struct ctdb_call *call,
925                                              struct ctdb_ltdb_header *header,
926                                              TDB_DATA *data);
927
928
929 int ctdbd_start(struct ctdb_context *ctdb);
930 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call);
931 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
932
933 /*
934   queue a packet for sending
935 */
936 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length);
937
938 /*
939   setup the fd used by the queue
940  */
941 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd);
942
943 /*
944   setup a packet queue on a socket
945  */
946 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
947                                     TALLOC_CTX *mem_ctx, int fd, int alignment,
948                                     
949                                     ctdb_queue_cb_fn_t callback,
950                                     void *private_data);
951
952 /*
953   allocate a packet for use in client<->daemon communication
954  */
955 struct ctdb_req_header *_ctdbd_allocate_pkt(struct ctdb_context *ctdb,
956                                             TALLOC_CTX *mem_ctx, 
957                                             enum ctdb_operation operation, 
958                                             size_t length, size_t slength,
959                                             const char *type);
960 #define ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, type) \
961         (type *)_ctdbd_allocate_pkt(ctdb, mem_ctx, operation, length, sizeof(type), #type)
962
963 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
964                                                  TALLOC_CTX *mem_ctx, 
965                                                  enum ctdb_operation operation, 
966                                                  size_t length, size_t slength,
967                                                  const char *type);
968 #define ctdb_transport_allocate(ctdb, mem_ctx, operation, length, type) \
969         (type *)_ctdb_transport_allocate(ctdb, mem_ctx, operation, length, sizeof(type), #type)
970
971 int ctdb_queue_length(struct ctdb_queue *queue);
972
973 /*
974   lock a record in the ltdb, given a key
975  */
976 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
977
978 /*
979   unlock a record in the ltdb, given a key
980  */
981 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key);
982
983
984 /*
985   make a ctdb call to the local daemon - async send. Called from client context.
986
987   This constructs a ctdb_call request and queues it for processing. 
988   This call never blocks.
989 */
990 struct ctdb_call_state *ctdb_client_call_send(struct ctdb_db_context *ctdb_db, 
991                                               struct ctdb_call *call);
992
993 /*
994   make a recv call to the local ctdb daemon - called from client context
995
996   This is called when the program wants to wait for a ctdb_call to complete and get the 
997   results. This call will block unless the call has already completed.
998 */
999 int ctdb_client_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
1000
1001 int ctdb_daemon_set_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
1002                              ctdb_message_fn_t handler,
1003                              void *private_data);
1004
1005 int ctdb_client_send_message(struct ctdb_context *ctdb, uint32_t vnn,
1006                              uint64_t srvid, TDB_DATA data);
1007
1008 /*
1009   send a ctdb message
1010 */
1011 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
1012                              uint64_t srvid, TDB_DATA data);
1013
1014
1015 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
1016                                       TDB_DATA key,
1017                                       void (*callback)(void *), void *private_data);
1018
1019 struct ctdb_call_state *ctdb_daemon_call_send(struct ctdb_db_context *ctdb_db, 
1020                                               struct ctdb_call *call);
1021
1022 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call);
1023
1024 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
1025                                                      struct ctdb_call *call, 
1026                                                      struct ctdb_ltdb_header *header);
1027
1028 int ctdb_call_local(struct ctdb_db_context *ctdb_db, struct ctdb_call *call,
1029                     struct ctdb_ltdb_header *header, TALLOC_CTX *mem_ctx, TDB_DATA *data,
1030                     uint32_t caller);
1031
1032 #define ctdb_reqid_find(ctdb, reqid, type)      (type *)_ctdb_reqid_find(ctdb, reqid, #type, __location__)
1033
1034 void ctdb_recv_raw_pkt(void *p, uint8_t *data, uint32_t length);
1035
1036 int ctdb_socket_connect(struct ctdb_context *ctdb);
1037
1038 void ctdb_latency(struct ctdb_db_context *ctdb_db, const char *name, double *latency, struct timeval t);
1039 void ctdb_reclock_latency(struct ctdb_context *ctdb, const char *name, double *latency, double l);
1040
1041 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state);
1042 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location);
1043 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid);
1044
1045 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
1046 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr);
1047
1048 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
1049                              uint64_t srvid, uint32_t opcode, uint32_t client_id, uint32_t flags,
1050                              TDB_DATA data,
1051                              ctdb_control_callback_fn_t callback,
1052                              void *private_data);
1053
1054 int32_t ctdb_control_db_attach(struct ctdb_context *ctdb, TDB_DATA indata, 
1055                                TDB_DATA *outdata, uint64_t tdb_flags, bool persistent);
1056
1057 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
1058                          ctdb_fn_t fn, int id);
1059
1060 int ctdb_control(struct ctdb_context *ctdb, uint32_t destnode, uint64_t srvid, 
1061                  uint32_t opcode, uint32_t flags, TDB_DATA data, 
1062                  TALLOC_CTX *mem_ctx, TDB_DATA *outdata, int32_t *status,
1063                  struct timeval *timeout, char **errormsg);
1064 int ctdb_control_recv(struct ctdb_context *ctdb, 
1065                 struct ctdb_client_control_state *state, 
1066                 TALLOC_CTX *mem_ctx,
1067                 TDB_DATA *outdata, int32_t *status, char **errormsg);
1068
1069 struct ctdb_client_control_state *
1070 ctdb_control_send(struct ctdb_context *ctdb, 
1071                 uint32_t destnode, uint64_t srvid, 
1072                 uint32_t opcode, uint32_t flags, TDB_DATA data, 
1073                 TALLOC_CTX *mem_ctx,
1074                 struct timeval *timeout,
1075                 char **errormsg);
1076
1077
1078
1079
1080 #define CHECK_CONTROL_DATA_SIZE(size) do { \
1081  if (indata.dsize != size) { \
1082          DEBUG(0,(__location__ " Invalid data size in opcode %u. Got %u expected %u\n", \
1083                   opcode, (unsigned)indata.dsize, (unsigned)size));     \
1084          return -1; \
1085  } \
1086  } while (0)
1087
1088 int ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1089 int ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1090 int ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1091 int ctdb_control_getnodemapv4(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1092 int ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1093 int ctdb_control_writerecord(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata);
1094
1095
1096 struct ctdb_traverse_start {
1097         uint32_t db_id;
1098         uint32_t reqid;
1099         uint64_t srvid;
1100 };
1101
1102 /*
1103   structure used to pass record data between the child and parent
1104  */
1105 struct ctdb_rec_data {
1106         uint32_t length;
1107         uint32_t reqid;
1108         uint32_t keylen;
1109         uint32_t datalen;
1110         uint8_t  data[1];
1111 };
1112                                    
1113
1114 /* structure used for pulldb control */
1115 struct ctdb_control_pulldb {
1116         uint32_t db_id;
1117         uint32_t lmaster;
1118 };
1119
1120 /* structure used for sending lists of records */
1121 struct ctdb_marshall_buffer {
1122         uint32_t db_id;
1123         uint32_t count;
1124         uint8_t data[1];
1125 };
1126
1127 /* set dmaster control structure */
1128 struct ctdb_control_set_dmaster {
1129         uint32_t db_id;
1130         uint32_t dmaster;
1131 };
1132
1133 /*
1134   structure for setting a tunable
1135  */
1136 struct ctdb_control_set_tunable {
1137         uint32_t value;
1138         uint32_t length;
1139         uint8_t  name[1];
1140 };
1141
1142 /*
1143   structure for getting a tunable
1144  */
1145 struct ctdb_control_get_tunable {
1146         uint32_t length;
1147         uint8_t  name[1];
1148 };
1149
1150 /*
1151   structure for listing tunables
1152  */
1153 struct ctdb_control_list_tunable {
1154         uint32_t length;
1155         /* returns a : separated list of tunable names */
1156         uint8_t  data[1];
1157 };
1158
1159
1160 /* table that contains a list of all nodes a ctdb knows about and their 
1161    status
1162  */
1163 struct ctdb_node_and_flags {
1164         uint32_t pnn;
1165         uint32_t flags;
1166         ctdb_sock_addr addr;
1167 };
1168
1169 struct ctdb_node_map {
1170         uint32_t num;
1171         struct ctdb_node_and_flags nodes[1];
1172 };
1173
1174 struct ctdb_node_and_flagsv4 {
1175         uint32_t pnn;
1176         uint32_t flags;
1177         struct sockaddr_in sin;
1178 };
1179
1180 struct ctdb_node_mapv4 {
1181         uint32_t num;
1182         struct ctdb_node_and_flagsv4 nodes[1];
1183 };
1184
1185 struct ctdb_control_wipe_database {
1186         uint32_t db_id;
1187         uint32_t transaction_id;
1188 };
1189
1190 /*
1191   state of a in-progress ctdb call in client
1192 */
1193 struct ctdb_client_call_state {
1194         enum call_state state;
1195         uint32_t reqid;
1196         struct ctdb_db_context *ctdb_db;
1197         struct ctdb_call *call;
1198         struct {
1199                 void (*fn)(struct ctdb_client_call_state *);
1200                 void *private_data;
1201         } async;
1202 };
1203
1204
1205 int32_t ctdb_control_traverse_start(struct ctdb_context *ctdb, TDB_DATA indata, 
1206                                     TDB_DATA *outdata, uint32_t srcnode, uint32_t client_id);
1207 int32_t ctdb_control_traverse_all(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1208 int32_t ctdb_control_traverse_data(struct ctdb_context *ctdb, TDB_DATA data, TDB_DATA *outdata);
1209 int32_t ctdb_control_traverse_kill(struct ctdb_context *ctdb, TDB_DATA indata, 
1210                                     TDB_DATA *outdata, uint32_t srcnode);
1211
1212 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data);
1213
1214 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1215 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data);
1216 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid);
1217
1218 int32_t ctdb_ltdb_enable_seqnum(struct ctdb_context *ctdb, uint32_t db_id);
1219 int32_t ctdb_ltdb_update_seqnum(struct ctdb_context *ctdb, uint32_t db_id, uint32_t srcnode);
1220
1221 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
1222                                            TDB_DATA key, struct ctdb_ltdb_header *, TDB_DATA data);
1223
1224 struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
1225                                               uint32_t *reqid,
1226                                               struct ctdb_ltdb_header *header,
1227                                               TDB_DATA *key, TDB_DATA *data);
1228
1229 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1230 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata);
1231 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata);
1232
1233 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
1234                                  struct ctdb_req_control *c,
1235                                  TDB_DATA indata, bool *async_reply,
1236                                  const char **errormsg);
1237 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
1238                                 TDB_DATA *outdata, int32_t status, const char *errormsg);
1239
1240 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1241 int32_t ctdb_control_thaw(struct ctdb_context *ctdb, uint32_t priority);
1242
1243 int ctdb_start_recoverd(struct ctdb_context *ctdb);
1244 void ctdb_stop_recoverd(struct ctdb_context *ctdb);
1245
1246 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb);
1247
1248 void ctdb_disable_monitoring(struct ctdb_context *ctdb);
1249 void ctdb_enable_monitoring(struct ctdb_context *ctdb);
1250 void ctdb_stop_monitoring(struct ctdb_context *ctdb);
1251 void ctdb_start_monitoring(struct ctdb_context *ctdb);
1252 void ctdb_start_tcp_tickle_update(struct ctdb_context *ctdb);
1253 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode);
1254 void ctdb_start_keepalive(struct ctdb_context *ctdb);
1255 void ctdb_stop_keepalive(struct ctdb_context *ctdb);
1256 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA data, bool *async_reply);
1257
1258
1259 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node);
1260 void ctdb_call_resend_all(struct ctdb_context *ctdb);
1261 void ctdb_node_dead(struct ctdb_node *node);
1262 void ctdb_node_connected(struct ctdb_node *node);
1263 bool ctdb_blocking_freeze(struct ctdb_context *ctdb);
1264 void ctdb_set_scheduler(struct ctdb_context *ctdb);
1265 void ctdb_restore_scheduler(struct ctdb_context *ctdb);
1266 int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, 
1267                                  struct ctdb_req_control *c,
1268                                  TDB_DATA indata, 
1269                                  bool *async_reply);
1270 int32_t ctdb_control_takeover_ipv4(struct ctdb_context *ctdb, 
1271                                  struct ctdb_req_control *c,
1272                                  TDB_DATA indata, 
1273                                  bool *async_reply);
1274 int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, 
1275                                  struct ctdb_req_control *c,
1276                                  TDB_DATA indata, 
1277                                  bool *async_reply);
1278 int32_t ctdb_control_release_ipv4(struct ctdb_context *ctdb, 
1279                                  struct ctdb_req_control *c,
1280                                  TDB_DATA indata, 
1281                                  bool *async_reply);
1282 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
1283                                  struct ctdb_req_control *c,
1284                                  bool *async_reply);
1285 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
1286                                  struct ctdb_req_control *c,
1287                                  bool *async_reply);
1288
1289 struct ctdb_public_ipv4 {
1290         uint32_t pnn;
1291         struct sockaddr_in sin;
1292 };
1293
1294 struct ctdb_public_ip {
1295         uint32_t pnn;
1296         ctdb_sock_addr addr;
1297 };
1298 int ctdb_ctrl_takeover_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1299                           uint32_t destnode, struct ctdb_public_ip *ip);
1300 int ctdb_ctrl_release_ip(struct ctdb_context *ctdb, struct timeval timeout, 
1301                          uint32_t destnode, struct ctdb_public_ip *ip);
1302
1303 struct ctdb_all_public_ipsv4 {
1304         uint32_t num;
1305         struct ctdb_public_ipv4 ips[1];
1306 };
1307
1308 struct ctdb_all_public_ips {
1309         uint32_t num;
1310         struct ctdb_public_ip ips[1];
1311 };
1312 int32_t ctdb_control_get_public_ipsv4(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1313 int32_t ctdb_control_get_public_ips(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA *outdata);
1314 int ctdb_ctrl_get_public_ips(struct ctdb_context *ctdb, 
1315                         struct timeval timeout, uint32_t destnode, 
1316                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1317 int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb, 
1318                         struct timeval timeout, uint32_t destnode, 
1319                         TALLOC_CTX *mem_ctx, struct ctdb_all_public_ips **ips);
1320
1321
1322 /* from takeover/system.c */
1323 uint32_t uint16_checksum(uint16_t *data, size_t n);
1324 int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface);
1325 bool ctdb_sys_have_ip(ctdb_sock_addr *addr);
1326 int ctdb_sys_send_tcp(const ctdb_sock_addr *dest, 
1327                       const ctdb_sock_addr *src,
1328                       uint32_t seq, uint32_t ack, int rst);
1329
1330 int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist);
1331 int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script);
1332 int ctdb_set_event_script_dir(struct ctdb_context *ctdb, const char *script_dir);
1333 int ctdb_set_notification_script(struct ctdb_context *ctdb, const char *script);
1334 int ctdb_takeover_run(struct ctdb_context *ctdb, struct ctdb_node_map *nodemap);
1335
1336 int32_t ctdb_control_tcp_client(struct ctdb_context *ctdb, uint32_t client_id, 
1337                                 TDB_DATA indata);
1338 int32_t ctdb_control_tcp_add(struct ctdb_context *ctdb, TDB_DATA indata);
1339 int32_t ctdb_control_tcp_remove(struct ctdb_context *ctdb, TDB_DATA indata);
1340 int32_t ctdb_control_startup(struct ctdb_context *ctdb, uint32_t vnn);
1341 int32_t ctdb_control_kill_tcp(struct ctdb_context *ctdb, TDB_DATA indata);
1342 int32_t ctdb_control_send_gratious_arp(struct ctdb_context *ctdb, TDB_DATA indata);
1343 int32_t ctdb_control_get_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1344 int32_t ctdb_control_set_tcp_tickle_list(struct ctdb_context *ctdb, TDB_DATA indata);
1345
1346 void ctdb_takeover_client_destructor_hook(struct ctdb_client *client);
1347 int ctdb_event_script(struct ctdb_context *ctdb, enum ctdb_eventscript_call call);
1348 int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call call,
1349                            const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1350 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
1351                                TALLOC_CTX *mem_ctx,
1352                                void (*callback)(struct ctdb_context *, int, void *),
1353                                void *private_data,
1354                                bool from_user,
1355                                enum ctdb_eventscript_call call,
1356                                const char *fmt, ...) PRINTF_ATTRIBUTE(7,8);
1357 void ctdb_release_all_ips(struct ctdb_context *ctdb);
1358
1359 void set_nonblocking(int fd);
1360 void set_close_on_exec(int fd);
1361
1362 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
1363
1364 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
1365
1366 int32_t ctdb_control_get_tunable(struct ctdb_context *ctdb, TDB_DATA indata, 
1367                                  TDB_DATA *outdata);
1368 int32_t ctdb_control_set_tunable(struct ctdb_context *ctdb, TDB_DATA indata);
1369 int32_t ctdb_control_list_tunables(struct ctdb_context *ctdb, TDB_DATA *outdata);
1370 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata);
1371 int32_t ctdb_control_add_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1372 int32_t ctdb_control_del_public_address(struct ctdb_context *ctdb, TDB_DATA indata);
1373
1374 void ctdb_tunables_set_defaults(struct ctdb_context *ctdb);
1375
1376 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata);
1377
1378 int ctdb_ctrl_get_all_tunables(struct ctdb_context *ctdb, 
1379                                struct timeval timeout, 
1380                                uint32_t destnode,
1381                                struct ctdb_tunable *tunables);
1382
1383 int ctdb_start_freeze(struct ctdb_context *ctdb, uint32_t priority);
1384
1385 bool parse_ip_mask(const char *s, const char *iface, ctdb_sock_addr *addr, unsigned *mask);
1386 bool parse_ip_port(const char *s, ctdb_sock_addr *addr);
1387 bool parse_ip(const char *s, const char *iface, unsigned port, ctdb_sock_addr *addr);
1388 bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin);
1389  
1390
1391 int ctdb_sys_open_capture_socket(const char *iface, void **private_data);
1392 int ctdb_sys_close_capture_socket(void *private_data);
1393 int ctdb_sys_read_tcp_packet(int s, void *private_data, ctdb_sock_addr *src, ctdb_sock_addr *dst, uint32_t *ack_seq, uint32_t *seq);
1394
1395 int ctdb_ctrl_killtcp(struct ctdb_context *ctdb, 
1396                       struct timeval timeout, 
1397                       uint32_t destnode,
1398                       struct ctdb_control_killtcp *killtcp);
1399
1400 int ctdb_ctrl_add_public_ip(struct ctdb_context *ctdb, 
1401                       struct timeval timeout, 
1402                       uint32_t destnode,
1403                       struct ctdb_control_ip_iface *pub);
1404
1405 int ctdb_ctrl_del_public_ip(struct ctdb_context *ctdb, 
1406                       struct timeval timeout, 
1407                       uint32_t destnode,
1408                       struct ctdb_control_ip_iface *pub);
1409
1410 int ctdb_ctrl_gratious_arp(struct ctdb_context *ctdb, 
1411                       struct timeval timeout, 
1412                       uint32_t destnode,
1413                       ctdb_sock_addr *addr,
1414                       const char *ifname);
1415
1416 int ctdb_ctrl_get_tcp_tickles(struct ctdb_context *ctdb, 
1417                       struct timeval timeout, 
1418                       uint32_t destnode,
1419                       TALLOC_CTX *mem_ctx,
1420                       ctdb_sock_addr *addr,
1421                       struct ctdb_control_tcp_tickle_list **list);
1422
1423
1424 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb, 
1425                       uint32_t client_id,
1426                       TDB_DATA indata);
1427 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb, 
1428                       TDB_DATA indata);
1429 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb, 
1430                       TDB_DATA indata);
1431 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, 
1432                       TDB_DATA *outdata);
1433 int32_t ctdb_control_uptime(struct ctdb_context *ctdb, 
1434                       TDB_DATA *outdata);
1435
1436 int ctdb_attach_persistent(struct ctdb_context *ctdb);
1437
1438 int32_t ctdb_control_persistent_store(struct ctdb_context *ctdb, 
1439                                       struct ctdb_req_control *c, 
1440                                       TDB_DATA recdata, bool *async_reply);
1441 int32_t ctdb_control_update_record(struct ctdb_context *ctdb, 
1442                                    struct ctdb_req_control *c, TDB_DATA recdata, 
1443                                    bool *async_reply);
1444 int32_t ctdb_control_trans2_commit(struct ctdb_context *ctdb, 
1445                                    struct ctdb_req_control *c, 
1446                                    TDB_DATA recdata, bool *async_reply);
1447
1448 int32_t ctdb_control_transaction_start(struct ctdb_context *ctdb, uint32_t id);
1449 int32_t ctdb_control_transaction_commit(struct ctdb_context *ctdb, uint32_t id);
1450 int32_t ctdb_control_transaction_cancel(struct ctdb_context *ctdb);
1451 int32_t ctdb_control_wipe_database(struct ctdb_context *ctdb, TDB_DATA indata);
1452
1453
1454 int ctdb_vacuum(struct ctdb_context *ctdb, int argc, const char **argv);
1455 int ctdb_repack(struct ctdb_context *ctdb, int argc, const char **argv);
1456
1457 void ctdb_block_signal(int signum);
1458 void ctdb_unblock_signal(int signum);
1459 int32_t ctdb_monitoring_mode(struct ctdb_context *ctdb);
1460 int ctdb_set_child_logging(struct ctdb_context *ctdb);
1461
1462
1463 typedef void (*client_async_callback)(struct ctdb_context *ctdb, uint32_t node_pnn, int32_t res, TDB_DATA outdata, void *callback_data);
1464
1465 struct client_async_data {
1466         enum ctdb_controls opcode;
1467         bool dont_log_errors;
1468         uint32_t count;
1469         uint32_t fail_count;
1470         client_async_callback callback;
1471         client_async_callback fail_callback;
1472         void *callback_data;
1473 };
1474 void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_control_state *state);
1475 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data);
1476 int ctdb_client_async_control(struct ctdb_context *ctdb,
1477                                 enum ctdb_controls opcode,
1478                                 uint32_t *nodes,
1479                                 uint64_t srvid,
1480                                 struct timeval timeout,
1481                                 bool dont_log_errors,
1482                                 TDB_DATA data,
1483                                 client_async_callback client_callback,
1484                                 client_async_callback fail_callback,
1485                                 void *callback_data);
1486
1487 void ctdb_load_nodes_file(struct ctdb_context *ctdb);
1488
1489 int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);
1490
1491 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata);
1492 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata);
1493
1494 int32_t ctdb_control_trans2_finished(struct ctdb_context *ctdb, 
1495                                      struct ctdb_req_control *c);
1496 int32_t ctdb_control_trans2_error(struct ctdb_context *ctdb, 
1497                                   struct ctdb_req_control *c);
1498 int32_t ctdb_control_trans2_active(struct ctdb_context *ctdb,
1499                                    struct ctdb_req_control *c,
1500                                    uint32_t db_id);
1501
1502 char *ctdb_addr_to_str(ctdb_sock_addr *addr);
1503 unsigned ctdb_addr_to_port(ctdb_sock_addr *addr);
1504 void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip);
1505
1506 int32_t ctdb_control_recd_ping(struct ctdb_context *ctdb);
1507 int32_t ctdb_control_set_recmaster(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata);
1508
1509 extern int script_log_level;
1510
1511 int ctdb_ctrl_event_script_init(struct ctdb_context *ctdb);
1512 int ctdb_ctrl_event_script_start(struct ctdb_context *ctdb, const char *name);
1513 int ctdb_ctrl_event_script_stop(struct ctdb_context *ctdb, int32_t res);
1514 int ctdb_ctrl_event_script_finished(struct ctdb_context *ctdb);
1515
1516 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb);
1517 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata);
1518 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata);
1519 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb);
1520
1521
1522 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata);
1523
1524 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len);
1525 int ctdb_ctrl_report_recd_lock_latency(struct ctdb_context *ctdb, struct timeval timeout, double latency);
1526
1527 int32_t ctdb_control_stop_node(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply);
1528 int32_t ctdb_control_continue_node(struct ctdb_context *ctdb);
1529
1530 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db);
1531
1532 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1533 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata);
1534
1535 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata);
1536 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata);
1537 int32_t ctdb_control_set_db_priority(struct ctdb_context *ctdb, TDB_DATA indata);
1538
1539 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1540
1541 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata);
1542
1543 int start_syslog_daemon(struct ctdb_context *ctdb);
1544
1545 /* Where to send the log messages back to */
1546 struct ctdb_get_log_addr {
1547         uint32_t pnn;
1548         uint64_t srvid;
1549         int32_t level;
1550 };
1551
1552 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr);
1553 int32_t ctdb_control_clear_log(struct ctdb_context *ctdb);
1554
1555 int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
1556 struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb, pid_t pid);
1557
1558 #endif