9f74011e0a58b76cd33a13ec7c66e2fd35589523
[vlendec/samba-autobuild/.git] / ctdb / server / ctdb_control.c
1 /* 
2    ctdb_control protocol code
3
4    Copyright (C) Andrew Tridgell  2007
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 #include "replace.h"
20 #include "system/network.h"
21 #include "system/filesys.h"
22 #include "system/wait.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26
27 #include "lib/tdb_wrap/tdb_wrap.h"
28 #include "lib/util/dlinklist.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/samba_util.h"
31 #include "lib/util/talloc_report.h"
32
33 #include "ctdb_private.h"
34 #include "ctdb_client.h"
35
36 #include "common/reqid.h"
37 #include "common/common.h"
38 #include "common/logging.h"
39
40
41 struct ctdb_control_state {
42         struct ctdb_context *ctdb;
43         uint32_t reqid;
44         ctdb_control_callback_fn_t callback;
45         void *private_data;
46         unsigned flags;
47 };
48
49
50 /*
51   dump talloc memory hierarchy, returning it as a blob to the client
52  */
53 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
54 {
55         char *report;
56         size_t reportlen;
57
58         report = talloc_report_str(outdata, NULL);
59         if (report == NULL) {
60                 DEBUG(DEBUG_ERR,
61                       (__location__ " talloc_report_str failed\n"));
62                 return -1;
63         }
64         reportlen = talloc_get_size(report);
65
66         if (reportlen > 0) {
67                 reportlen -= 1; /* strip trailing zero */
68         }
69
70         outdata->dptr = (uint8_t *)report;
71         outdata->dsize = reportlen;
72         return 0;
73 }
74
75 static int32_t control_not_implemented(const char *unsupported,
76                                        const char *alternate)
77 {
78         if (alternate == NULL) {
79                 DEBUG(DEBUG_ERR,
80                       ("Control %s is not implemented any more\n",
81                        unsupported));
82         } else {
83                 DEBUG(DEBUG_ERR,
84                       ("Control %s is not implemented any more, use %s instead\n",
85                        unsupported, alternate));
86         }
87         return -1;
88 }
89
90 /*
91   process a control request
92  */
93 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb, 
94                                      struct ctdb_req_control_old *c,
95                                      TDB_DATA indata,
96                                      TDB_DATA *outdata, uint32_t srcnode,
97                                      const char **errormsg,
98                                      bool *async_reply)
99 {
100         uint32_t opcode = c->opcode;
101         uint64_t srvid = c->srvid;
102         uint32_t client_id = c->client_id;
103
104         switch (opcode) {
105         case CTDB_CONTROL_PROCESS_EXISTS: {
106                 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
107                 return ctdb_control_process_exists(ctdb, *(pid_t *)indata.dptr);
108         }
109
110         case CTDB_CONTROL_SET_DEBUG: {
111                 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
112                 DEBUGLEVEL = *(int32_t *)indata.dptr;
113                 return 0;
114         }
115
116         case CTDB_CONTROL_GET_DEBUG: {
117                 CHECK_CONTROL_DATA_SIZE(0);
118                 outdata->dptr = (uint8_t *)&(DEBUGLEVEL);
119                 outdata->dsize = sizeof(DEBUGLEVEL);
120                 return 0;
121         }
122
123         case CTDB_CONTROL_STATISTICS: {
124                 CHECK_CONTROL_DATA_SIZE(0);
125                 ctdb->statistics.memory_used = talloc_total_size(NULL);
126                 ctdb->statistics.num_clients = ctdb->num_clients;
127                 ctdb->statistics.frozen = (ctdb_db_all_frozen(ctdb) ? 1 : 0);
128                 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
129                 ctdb->statistics.statistics_current_time = timeval_current();
130
131                 outdata->dptr = (uint8_t *)&ctdb->statistics;
132                 outdata->dsize = sizeof(ctdb->statistics);
133                 return 0;
134         }
135
136         case CTDB_CONTROL_GET_ALL_TUNABLES: {
137                 CHECK_CONTROL_DATA_SIZE(0);
138                 outdata->dptr = (uint8_t *)&ctdb->tunable;
139                 outdata->dsize = sizeof(ctdb->tunable);
140                 return 0;
141         }
142
143         case CTDB_CONTROL_DUMP_MEMORY: {
144                 CHECK_CONTROL_DATA_SIZE(0);
145                 return ctdb_dump_memory(ctdb, outdata);
146         }
147
148         case CTDB_CONTROL_STATISTICS_RESET: {
149                 struct ctdb_db_context *ctdb_db;
150
151                 CHECK_CONTROL_DATA_SIZE(0);
152                 ZERO_STRUCT(ctdb->statistics);
153                 for (ctdb_db = ctdb->db_list;
154                      ctdb_db != NULL;
155                      ctdb_db = ctdb_db->next) {
156                         ctdb_db_statistics_reset(ctdb_db);
157                 }
158                 ctdb->statistics.statistics_start_time = timeval_current();
159                 return 0;
160         }
161
162         case CTDB_CONTROL_GETVNNMAP:
163                 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
164
165         case CTDB_CONTROL_GET_DBMAP:
166                 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
167
168         case CTDB_CONTROL_GET_NODEMAPv4:
169                 return control_not_implemented("GET_NODEMAPv4", "GET_NODEMAP");
170
171         case CTDB_CONTROL_GET_NODEMAP:
172                 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
173
174         case CTDB_CONTROL_GET_NODES_FILE:
175                 return ctdb_control_getnodesfile(ctdb, opcode, indata, outdata);
176
177         case CTDB_CONTROL_RELOAD_NODES_FILE:
178                 CHECK_CONTROL_DATA_SIZE(0);
179                 return ctdb_control_reload_nodes_file(ctdb, opcode);
180
181         case CTDB_CONTROL_SET_DB_STICKY: {
182                 uint32_t db_id;
183                 struct ctdb_db_context *ctdb_db;
184
185                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
186                 db_id = *(uint32_t *)indata.dptr;
187                 ctdb_db = find_ctdb_db(ctdb, db_id);
188                 if (ctdb_db == NULL) return -1;
189                 return ctdb_set_db_sticky(ctdb, ctdb_db);
190         }
191
192         case CTDB_CONTROL_SETVNNMAP:
193                 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
194
195         case CTDB_CONTROL_PULL_DB: 
196                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb));
197                 return ctdb_control_pull_db(ctdb, indata, outdata);
198
199         case CTDB_CONTROL_SET_DMASTER: 
200                 return control_not_implemented("SET_DMASTER", NULL);
201
202         case CTDB_CONTROL_PUSH_DB:
203                 return ctdb_control_push_db(ctdb, indata);
204
205         case CTDB_CONTROL_GET_RECMODE: {
206                 return ctdb->recovery_mode;
207         }
208
209         case CTDB_CONTROL_SET_RECMASTER: {
210                 return ctdb_control_set_recmaster(ctdb, opcode, indata);
211         }
212
213         case CTDB_CONTROL_GET_RECMASTER:
214                 return ctdb->recovery_master;
215
216         case CTDB_CONTROL_GET_PID:
217                 return getpid();
218
219         case CTDB_CONTROL_GET_PNN:
220                 return ctdb->pnn;
221
222         case CTDB_CONTROL_PING:
223                 CHECK_CONTROL_DATA_SIZE(0);
224                 return ctdb->num_clients;
225
226         case CTDB_CONTROL_GET_RUNSTATE:
227                 CHECK_CONTROL_DATA_SIZE(0);
228                 outdata->dptr = (uint8_t *)&ctdb->runstate;
229                 outdata->dsize = sizeof(uint32_t);
230                 return 0;
231
232
233         case CTDB_CONTROL_SET_DB_READONLY: {
234                 uint32_t db_id;
235                 struct ctdb_db_context *ctdb_db;
236
237                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
238                 db_id = *(uint32_t *)indata.dptr;
239                 ctdb_db = find_ctdb_db(ctdb, db_id);
240                 if (ctdb_db == NULL) return -1;
241                 return ctdb_set_db_readonly(ctdb, ctdb_db);
242         }
243         case CTDB_CONTROL_GET_DBNAME: {
244                 uint32_t db_id;
245                 struct ctdb_db_context *ctdb_db;
246
247                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
248                 db_id = *(uint32_t *)indata.dptr;
249                 ctdb_db = find_ctdb_db(ctdb, db_id);
250                 if (ctdb_db == NULL) return -1;
251                 outdata->dptr = discard_const(ctdb_db->db_name);
252                 outdata->dsize = strlen(ctdb_db->db_name)+1;
253                 return 0;
254         }
255
256         case CTDB_CONTROL_GETDBPATH: {
257                 uint32_t db_id;
258                 struct ctdb_db_context *ctdb_db;
259
260                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
261                 db_id = *(uint32_t *)indata.dptr;
262                 ctdb_db = find_ctdb_db(ctdb, db_id);
263                 if (ctdb_db == NULL) return -1;
264                 outdata->dptr = discard_const(ctdb_db->db_path);
265                 outdata->dsize = strlen(ctdb_db->db_path)+1;
266                 return 0;
267         }
268
269         case CTDB_CONTROL_DB_ATTACH:
270           return ctdb_control_db_attach(ctdb, indata, outdata, 0, client_id,
271                                         c, async_reply);
272
273         case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
274           return ctdb_control_db_attach(ctdb, indata, outdata,
275                                         CTDB_DB_FLAGS_PERSISTENT, client_id,
276                                         c, async_reply);
277
278         case CTDB_CONTROL_DB_ATTACH_REPLICATED:
279           return ctdb_control_db_attach(ctdb, indata, outdata,
280                                         CTDB_DB_FLAGS_REPLICATED, client_id,
281                                         c, async_reply);
282
283         case CTDB_CONTROL_SET_CALL:
284                 return control_not_implemented("SET_CALL", NULL);
285
286         case CTDB_CONTROL_TRAVERSE_START:
287                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
288                 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
289
290         case CTDB_CONTROL_TRAVERSE_START_EXT:
291                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start_ext));
292                 return ctdb_control_traverse_start_ext(ctdb, indata, outdata, srcnode, client_id);
293
294         case CTDB_CONTROL_TRAVERSE_ALL:
295                 return ctdb_control_traverse_all(ctdb, indata, outdata);
296
297         case CTDB_CONTROL_TRAVERSE_ALL_EXT:
298                 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
299
300         case CTDB_CONTROL_TRAVERSE_DATA:
301                 return ctdb_control_traverse_data(ctdb, indata, outdata);
302
303         case CTDB_CONTROL_TRAVERSE_KILL:
304                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
305                 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
306
307         case CTDB_CONTROL_REGISTER_SRVID:
308                 return daemon_register_message_handler(ctdb, client_id, srvid);
309
310         case CTDB_CONTROL_DEREGISTER_SRVID:
311                 return daemon_deregister_message_handler(ctdb, client_id, srvid);
312
313         case CTDB_CONTROL_CHECK_SRVIDS:
314                 return control_not_implemented("CHECK_SRVIDS", NULL);
315
316         case CTDB_CONTROL_ENABLE_SEQNUM:
317                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
318                 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
319
320         case CTDB_CONTROL_UPDATE_SEQNUM:
321                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
322                 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
323
324         case CTDB_CONTROL_FREEZE:
325                 CHECK_CONTROL_DATA_SIZE(0);
326                 return ctdb_control_freeze(ctdb, c, async_reply);
327
328         case CTDB_CONTROL_THAW:
329                 return control_not_implemented("THAW", NULL);
330
331         case CTDB_CONTROL_SET_RECMODE:
332                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
333                 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
334
335         case CTDB_CONTROL_GET_MONMODE:
336                 return control_not_implemented("GET_MONMODE", NULL);
337
338         case CTDB_CONTROL_ENABLE_MONITOR:
339                 return control_not_implemented("ENABLE_MONITOR", NULL);
340
341         case CTDB_CONTROL_RUN_EVENTSCRIPTS:
342                 return control_not_implemented("RUN_EVENTSCRIPTS", NULL);
343
344         case CTDB_CONTROL_DISABLE_MONITOR:
345                 return control_not_implemented("DISABLE_MONITOR", NULL);
346
347         case CTDB_CONTROL_SHUTDOWN:
348                 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
349                 ctdb_shutdown_sequence(ctdb, 0);
350                 /* In case above returns due to duplicate shutdown */
351                 return 0;
352
353         case CTDB_CONTROL_TAKEOVER_IPv4:
354                 return control_not_implemented("TAKEOVER_IPv4", "TAKEOVER_IP");
355
356         case CTDB_CONTROL_TAKEOVER_IP:
357                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
358                 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
359
360         case CTDB_CONTROL_RELEASE_IPv4:
361                 return control_not_implemented("RELEASE_IPv4", "RELEASE_IP");
362
363         case CTDB_CONTROL_RELEASE_IP:
364                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
365                 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
366
367         case CTDB_CONTROL_IPREALLOCATED:
368                 CHECK_CONTROL_DATA_SIZE(0);
369                 return ctdb_control_ipreallocated(ctdb, c, async_reply);
370
371         case CTDB_CONTROL_GET_PUBLIC_IPSv4:
372                 return control_not_implemented("GET_PUBLIC_IPSv4",
373                                                "GET_PUBLIC_IPS");
374
375         case CTDB_CONTROL_GET_PUBLIC_IPS:
376                 CHECK_CONTROL_DATA_SIZE(0);
377                 return ctdb_control_get_public_ips(ctdb, c, outdata);
378
379         case CTDB_CONTROL_TCP_CLIENT:
380                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
381                 return ctdb_control_tcp_client(ctdb, client_id, indata);
382
383         case CTDB_CONTROL_STARTUP: 
384                 CHECK_CONTROL_DATA_SIZE(0);
385                 return ctdb_control_startup(ctdb, srcnode);
386
387         case CTDB_CONTROL_TCP_ADD: 
388                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
389                 return ctdb_control_tcp_add(ctdb, indata, false);
390
391         case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE: 
392                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
393                 return ctdb_control_tcp_add(ctdb, indata, true);
394
395         case CTDB_CONTROL_TCP_REMOVE: 
396                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
397                 return ctdb_control_tcp_remove(ctdb, indata);
398
399         case CTDB_CONTROL_SET_TUNABLE:
400                 return ctdb_control_set_tunable(ctdb, indata);
401
402         case CTDB_CONTROL_GET_TUNABLE:
403                 return ctdb_control_get_tunable(ctdb, indata, outdata);
404
405         case CTDB_CONTROL_LIST_TUNABLES:
406                 return ctdb_control_list_tunables(ctdb, outdata);
407
408         case CTDB_CONTROL_MODIFY_FLAGS:
409                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
410                 return ctdb_control_modflags(ctdb, indata);
411
412         case CTDB_CONTROL_KILL_TCP:
413                 return control_not_implemented("KILL_TCP", NULL);
414
415         case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
416                 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
417                 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
418
419         case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
420                 /* data size is verified in the called function */
421                 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
422
423         case CTDB_CONTROL_REGISTER_SERVER_ID:
424                 return control_not_implemented("REGISTER_SERVER_ID", NULL);
425
426         case CTDB_CONTROL_UNREGISTER_SERVER_ID:
427                 return control_not_implemented("UNREGISTER_SERVER_ID", NULL);
428
429         case CTDB_CONTROL_CHECK_SERVER_ID:
430                 return control_not_implemented("CHECK_SERVER_ID", NULL);
431
432         case CTDB_CONTROL_GET_SERVER_ID_LIST:
433                 return control_not_implemented("SERVER_ID_LIST", NULL);
434
435         case CTDB_CONTROL_PERSISTENT_STORE:
436                 return control_not_implemented("PERSISTENT_STORE", NULL);
437
438         case CTDB_CONTROL_UPDATE_RECORD:
439                 return ctdb_control_update_record(ctdb, c, indata, async_reply);
440
441         case CTDB_CONTROL_SEND_GRATUITOUS_ARP:
442                 return ctdb_control_send_gratious_arp(ctdb, indata);
443
444         case CTDB_CONTROL_TRANSACTION_START:
445                 return control_not_implemented("TRANSACTION_START", NULL);
446
447         case CTDB_CONTROL_TRANSACTION_COMMIT:
448                 return control_not_implemented("TRANSACTION_COMMIT", NULL);
449
450         case CTDB_CONTROL_WIPE_DATABASE:
451                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
452                 return ctdb_control_wipe_database(ctdb, indata);
453
454         case CTDB_CONTROL_UPTIME:
455                 return ctdb_control_uptime(ctdb, outdata);
456
457         case CTDB_CONTROL_START_RECOVERY:
458                 return ctdb_control_start_recovery(ctdb, c, async_reply);
459
460         case CTDB_CONTROL_END_RECOVERY:
461                 return ctdb_control_end_recovery(ctdb, c, async_reply);
462
463         case CTDB_CONTROL_TRY_DELETE_RECORDS:
464                 return ctdb_control_try_delete_records(ctdb, indata, outdata);
465
466         case CTDB_CONTROL_ADD_PUBLIC_IP:
467                 return ctdb_control_add_public_address(ctdb, indata);
468
469         case CTDB_CONTROL_DEL_PUBLIC_IP:
470                 return ctdb_control_del_public_address(ctdb, indata);
471
472         case CTDB_CONTROL_GET_CAPABILITIES:
473                 return ctdb_control_get_capabilities(ctdb, outdata);
474
475         case CTDB_CONTROL_START_PERSISTENT_UPDATE:
476                 return ctdb_control_start_persistent_update(ctdb, c, indata);
477
478         case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
479                 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
480
481         case CTDB_CONTROL_TRANS2_COMMIT:
482         case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
483                 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
484
485         case CTDB_CONTROL_TRANS2_ERROR:
486                 return control_not_implemented("TRANS2_ERROR", NULL);
487
488         case CTDB_CONTROL_TRANS2_FINISHED:
489                 return control_not_implemented("TRANS2_FINISHED", NULL);
490
491         case CTDB_CONTROL_TRANS2_ACTIVE:
492                 return control_not_implemented("TRANS2_ACTIVE", NULL);
493
494         case CTDB_CONTROL_TRANS3_COMMIT:
495                 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
496
497         case CTDB_CONTROL_RECD_PING:
498                 CHECK_CONTROL_DATA_SIZE(0);
499                 return ctdb_control_recd_ping(ctdb);
500
501         case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
502                 return control_not_implemented("GET_EVENT_SCRIPT_STATUS", NULL);
503
504         case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
505                 CHECK_CONTROL_DATA_SIZE(sizeof(double));
506                 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
507                 return 0;
508         case CTDB_CONTROL_GET_RECLOCK_FILE:
509                 CHECK_CONTROL_DATA_SIZE(0);
510                 if (ctdb->recovery_lock != NULL) {
511                         outdata->dptr  = discard_const(ctdb->recovery_lock);
512                         outdata->dsize = strlen(ctdb->recovery_lock) + 1;
513                 }
514                 return 0;
515         case CTDB_CONTROL_SET_RECLOCK_FILE:
516                 return control_not_implemented("SET_RECLOCK", NULL);
517
518         case CTDB_CONTROL_STOP_NODE:
519                 CHECK_CONTROL_DATA_SIZE(0);
520                 return ctdb_control_stop_node(ctdb);
521
522         case CTDB_CONTROL_CONTINUE_NODE:
523                 CHECK_CONTROL_DATA_SIZE(0);
524                 return ctdb_control_continue_node(ctdb);
525
526         case CTDB_CONTROL_SET_NATGWSTATE:
527                 return control_not_implemented("SET_NATGWSTATE", NULL);
528
529         case CTDB_CONTROL_SET_LMASTERROLE: {
530                 uint32_t lmasterrole;
531
532                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
533                 lmasterrole = *(uint32_t *)indata.dptr;
534                 if (lmasterrole == 0) {
535                         ctdb->capabilities &= ~CTDB_CAP_LMASTER;
536                 } else {
537                         ctdb->capabilities |= CTDB_CAP_LMASTER;
538                 }
539                 return 0;
540         }
541
542         case CTDB_CONTROL_SET_RECMASTERROLE: {
543                 uint32_t recmasterrole;
544
545                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
546                 recmasterrole = *(uint32_t *)indata.dptr;
547                 if (recmasterrole == 0) {
548                         ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
549                 } else {
550                         ctdb->capabilities |= CTDB_CAP_RECMASTER;
551                 }
552                 return 0;
553         }
554
555         case CTDB_CONTROL_ENABLE_SCRIPT:
556                 return control_not_implemented("ENABLE_SCRIPT", NULL);
557
558         case CTDB_CONTROL_DISABLE_SCRIPT:
559                 return control_not_implemented("DISABLE_SCRIPT", NULL);
560
561         case CTDB_CONTROL_SET_BAN_STATE:
562                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_state));
563                 return ctdb_control_set_ban_state(ctdb, indata);
564
565         case CTDB_CONTROL_GET_BAN_STATE:
566                 CHECK_CONTROL_DATA_SIZE(0);
567                 return ctdb_control_get_ban_state(ctdb, outdata);
568
569         case CTDB_CONTROL_SET_DB_PRIORITY:
570                 return control_not_implemented("SET_DB_PRIORITY", NULL);
571
572         case CTDB_CONTROL_GET_DB_PRIORITY:
573                 return control_not_implemented("GET_DB_PRIORITY", NULL);
574
575         case CTDB_CONTROL_TRANSACTION_CANCEL:
576                 return control_not_implemented("TRANSACTION_CANCEL", NULL);
577
578         case CTDB_CONTROL_REGISTER_NOTIFY:
579                 return ctdb_control_register_notify(ctdb, client_id, indata);
580
581         case CTDB_CONTROL_DEREGISTER_NOTIFY:
582                 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
583                 return ctdb_control_deregister_notify(ctdb, client_id, indata);
584
585         case CTDB_CONTROL_GET_LOG:
586                 return control_not_implemented("GET_LOG", NULL);
587
588         case CTDB_CONTROL_CLEAR_LOG:
589                 return control_not_implemented("CLEAR_LOG", NULL);
590
591         case CTDB_CONTROL_GET_DB_SEQNUM:
592                 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
593                 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
594
595         case CTDB_CONTROL_DB_SET_HEALTHY:
596                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
597                 return ctdb_control_db_set_healthy(ctdb, indata);
598
599         case CTDB_CONTROL_DB_GET_HEALTH:
600                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
601                 return ctdb_control_db_get_health(ctdb, indata, outdata);
602
603         case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
604                 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
605                 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
606
607         case CTDB_CONTROL_GET_IFACES:
608                 CHECK_CONTROL_DATA_SIZE(0);
609                 return ctdb_control_get_ifaces(ctdb, c, outdata);
610
611         case CTDB_CONTROL_SET_IFACE_LINK_STATE:
612                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_iface));
613                 return ctdb_control_set_iface_link(ctdb, c, indata);
614
615         case CTDB_CONTROL_GET_STAT_HISTORY:
616                 CHECK_CONTROL_DATA_SIZE(0);
617                 return ctdb_control_get_stat_history(ctdb, c, outdata);
618
619         case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
620                 struct ctdb_control_schedule_for_deletion *d;
621                 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
622                 CHECK_CONTROL_MIN_DATA_SIZE(size);
623                 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
624                 size += d->keylen;
625                 CHECK_CONTROL_DATA_SIZE(size);
626                 return ctdb_control_schedule_for_deletion(ctdb, indata);
627         }
628         case CTDB_CONTROL_GET_DB_STATISTICS:
629                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
630                 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
631
632         case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
633                 CHECK_CONTROL_DATA_SIZE(0);
634                 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
635
636         case CTDB_CONTROL_RECEIVE_RECORDS:
637                 return ctdb_control_receive_records(ctdb, indata, outdata);
638
639         case CTDB_CONTROL_DB_DETACH:
640                 return ctdb_control_db_detach(ctdb, indata, client_id);
641
642         case CTDB_CONTROL_DB_FREEZE:
643                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
644                 return ctdb_control_db_freeze(ctdb, c, *(uint32_t *)indata.dptr,
645                                               async_reply);
646
647         case CTDB_CONTROL_DB_THAW:
648                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
649                 return ctdb_control_db_thaw(ctdb, *(uint32_t *)indata.dptr);
650
651         case CTDB_CONTROL_DB_TRANSACTION_START:
652                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
653                 return ctdb_control_db_transaction_start(ctdb, indata);
654
655         case CTDB_CONTROL_DB_TRANSACTION_COMMIT:
656                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
657                 return ctdb_control_db_transaction_commit(ctdb, indata);
658
659         case CTDB_CONTROL_DB_TRANSACTION_CANCEL:
660                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
661                 return ctdb_control_db_transaction_cancel(ctdb, indata);
662
663         case CTDB_CONTROL_DB_PULL:
664                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
665                 return ctdb_control_db_pull(ctdb, c, indata, outdata);
666
667         case CTDB_CONTROL_DB_PUSH_START:
668                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb_ext));
669                 return ctdb_control_db_push_start(ctdb, indata);
670
671         case CTDB_CONTROL_DB_PUSH_CONFIRM:
672                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
673                 return ctdb_control_db_push_confirm(ctdb, indata, outdata);
674
675         case CTDB_CONTROL_DB_OPEN_FLAGS: {
676                 uint32_t db_id;
677                 struct ctdb_db_context *ctdb_db;
678                 int tdb_flags;
679
680                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
681                 db_id = *(uint32_t *)indata.dptr;
682                 ctdb_db = find_ctdb_db(ctdb, db_id);
683                 if (ctdb_db == NULL) {
684                         return -1;
685                 }
686
687                 tdb_flags = tdb_get_flags(ctdb_db->ltdb->tdb);
688
689                 outdata->dptr = talloc_size(outdata, sizeof(tdb_flags));
690                 if (outdata->dptr == NULL) {
691                         return -1;
692                 }
693
694                 outdata->dsize = sizeof(tdb_flags);
695                 memcpy(outdata->dptr, &tdb_flags, outdata->dsize);
696                 return 0;
697         }
698
699         default:
700                 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
701                 return -1;
702         }
703 }
704
705 /*
706   send a reply for a ctdb control
707  */
708 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control_old *c,
709                                 TDB_DATA *outdata, int32_t status, const char *errormsg)
710 {
711         struct ctdb_reply_control_old *r;
712         size_t len;
713         
714         /* some controls send no reply */
715         if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
716                 return;
717         }
718
719         len = offsetof(struct ctdb_reply_control_old, data) + (outdata?outdata->dsize:0);
720         if (errormsg) {
721                 len += strlen(errormsg);
722         }
723         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control_old);
724         if (r == NULL) {
725                 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
726                 return;
727         }
728
729         r->hdr.destnode     = c->hdr.srcnode;
730         r->hdr.reqid        = c->hdr.reqid;
731         r->status           = status;
732         r->datalen          = outdata?outdata->dsize:0;
733         if (outdata && outdata->dsize) {
734                 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
735         }
736         if (errormsg) {
737                 r->errorlen = strlen(errormsg);
738                 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
739         }
740
741         ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);     
742
743         talloc_free(r);
744 }
745
746 /*
747   called when a CTDB_REQ_CONTROL packet comes in
748 */
749 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
750 {
751         struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr;
752         TDB_DATA data, *outdata;
753         int32_t status;
754         bool async_reply = false;
755         const char *errormsg = NULL;
756
757         data.dptr = &c->data[0];
758         data.dsize = c->datalen;
759
760         outdata = talloc_zero(c, TDB_DATA);
761
762         status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode, 
763                                        &errormsg, &async_reply);
764
765         if (!async_reply) {
766                 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
767         }
768 }
769
770 /*
771   called when a CTDB_REPLY_CONTROL packet comes in
772 */
773 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
774 {
775         struct ctdb_reply_control_old *c = (struct ctdb_reply_control_old *)hdr;
776         TDB_DATA data;
777         struct ctdb_control_state *state;
778         const char *errormsg = NULL;
779
780         state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_control_state);
781         if (state == NULL) {
782                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
783                          ctdb->pnn, hdr->reqid));
784                 return;
785         }
786
787         if (hdr->reqid != state->reqid) {
788                 /* we found a record  but it was the wrong one */
789                 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
790                 return;
791         }
792
793         data.dptr = &c->data[0];
794         data.dsize = c->datalen;
795         if (c->errorlen) {
796                 errormsg = talloc_strndup(state, 
797                                           (char *)&c->data[c->datalen], c->errorlen);
798         }
799
800         /* make state a child of the packet, so it goes away when the packet
801            is freed. */
802         talloc_steal(hdr, state);
803
804         state->callback(ctdb, c->status, data, errormsg, state->private_data);
805 }
806
807 static int ctdb_control_destructor(struct ctdb_control_state *state)
808 {
809         reqid_remove(state->ctdb->idr, state->reqid);
810         return 0;
811 }
812
813 /*
814   handle a timeout of a control
815  */
816 static void ctdb_control_timeout(struct tevent_context *ev,
817                                  struct tevent_timer *te,
818                                  struct timeval t, void *private_data)
819 {
820         struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
821         TALLOC_CTX *tmp_ctx = talloc_new(ev);
822
823         CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
824
825         talloc_steal(tmp_ctx, state);
826
827         state->callback(state->ctdb, -1, tdb_null,
828                         "ctdb_control timed out", 
829                         state->private_data);
830         talloc_free(tmp_ctx);
831 }
832
833
834 /*
835   send a control message to a node
836  */
837 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
838                              uint64_t srvid, uint32_t opcode, uint32_t client_id,
839                              uint32_t flags,
840                              TDB_DATA data,
841                              ctdb_control_callback_fn_t callback,
842                              void *private_data)
843 {
844         struct ctdb_req_control_old *c;
845         struct ctdb_control_state *state;
846         size_t len;
847
848         if (ctdb->methods == NULL) {
849                 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
850                 return -1;
851         }
852
853         if (((destnode == CTDB_BROADCAST_VNNMAP) || 
854              (destnode == CTDB_BROADCAST_ALL) ||
855              (destnode == CTDB_BROADCAST_CONNECTED)) && 
856             !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
857                 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
858                 return -1;
859         }
860
861         if (destnode != CTDB_BROADCAST_VNNMAP && 
862             destnode != CTDB_BROADCAST_ALL && 
863             destnode != CTDB_BROADCAST_CONNECTED && 
864             (!ctdb_validate_pnn(ctdb, destnode) || 
865              (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
866                 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
867                         callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
868                 }
869                 return 0;
870         }
871
872         /* the state is made a child of private_data if possible. This means any reply
873            will be discarded if the private_data goes away */
874         state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
875         CTDB_NO_MEMORY(ctdb, state);
876
877         state->reqid = reqid_new(ctdb->idr, state);
878         state->callback = callback;
879         state->private_data = private_data;
880         state->ctdb = ctdb;
881         state->flags = flags;
882
883         talloc_set_destructor(state, ctdb_control_destructor);
884
885         len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
886         c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len, 
887                                     struct ctdb_req_control_old);
888         CTDB_NO_MEMORY(ctdb, c);
889         talloc_set_name_const(c, "ctdb_req_control packet");
890
891         c->hdr.destnode     = destnode;
892         c->hdr.reqid        = state->reqid;
893         c->opcode           = opcode;
894         c->client_id        = client_id;
895         c->flags            = flags;
896         c->srvid            = srvid;
897         c->datalen          = data.dsize;
898         if (data.dsize) {
899                 memcpy(&c->data[0], data.dptr, data.dsize);
900         }
901
902         ctdb_queue_packet(ctdb, &c->hdr);       
903
904         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
905                 talloc_free(state);
906                 return 0;
907         }
908
909         if (ctdb->tunable.control_timeout) {
910                 tevent_add_timer(ctdb->ev, state,
911                                  timeval_current_ofs(ctdb->tunable.control_timeout, 0),
912                                  ctdb_control_timeout, state);
913         }
914
915         talloc_free(c);
916         return 0;
917 }