ldb: Release ldb 1.3.0
[Samba.git] / ctdb / server / ctdb_control.c
blob9aeaa235830f8a2461efdd2fa1d2799c2eceb7fc
1 /*
2 ctdb_control protocol code
4 Copyright (C) Andrew Tridgell 2007
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.
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.
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/>.
19 #include "replace.h"
20 #include "system/network.h"
21 #include "system/filesys.h"
22 #include "system/wait.h"
24 #include <talloc.h>
25 #include <tevent.h>
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"
33 #include "ctdb_private.h"
34 #include "ctdb_client.h"
36 #include "common/reqid.h"
37 #include "common/common.h"
38 #include "common/logging.h"
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;
51 dump talloc memory hierarchy, returning it as a blob to the client
53 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
55 char *report;
56 size_t reportlen;
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;
64 reportlen = talloc_get_size(report);
66 if (reportlen > 0) {
67 reportlen -= 1; /* strip trailing zero */
70 outdata->dptr = (uint8_t *)report;
71 outdata->dsize = reportlen;
72 return 0;
75 static int32_t control_not_implemented(const char *unsupported,
76 const char *alternate)
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));
87 return -1;
91 process a control request
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)
100 uint32_t opcode = c->opcode;
101 uint64_t srvid = c->srvid;
102 uint32_t client_id = c->client_id;
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);
110 case CTDB_CONTROL_SET_DEBUG: {
111 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
112 DEBUGLEVEL = *(int32_t *)indata.dptr;
113 return 0;
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;
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();
131 outdata->dptr = (uint8_t *)&ctdb->statistics;
132 outdata->dsize = sizeof(ctdb->statistics);
133 return 0;
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;
143 case CTDB_CONTROL_DUMP_MEMORY: {
144 CHECK_CONTROL_DATA_SIZE(0);
145 return ctdb_dump_memory(ctdb, outdata);
148 case CTDB_CONTROL_STATISTICS_RESET: {
149 struct ctdb_db_context *ctdb_db;
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);
158 ctdb->statistics.statistics_start_time = timeval_current();
159 return 0;
162 case CTDB_CONTROL_GETVNNMAP:
163 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
165 case CTDB_CONTROL_GET_DBMAP:
166 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
168 case CTDB_CONTROL_GET_NODEMAPv4:
169 return control_not_implemented("GET_NODEMAPv4", "GET_NODEMAP");
171 case CTDB_CONTROL_GET_NODEMAP:
172 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
174 case CTDB_CONTROL_GET_NODES_FILE:
175 return ctdb_control_getnodesfile(ctdb, opcode, indata, outdata);
177 case CTDB_CONTROL_RELOAD_NODES_FILE:
178 CHECK_CONTROL_DATA_SIZE(0);
179 return ctdb_control_reload_nodes_file(ctdb, opcode);
181 case CTDB_CONTROL_SET_DB_STICKY: {
182 uint32_t db_id;
183 struct ctdb_db_context *ctdb_db;
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);
192 case CTDB_CONTROL_SETVNNMAP:
193 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
195 case CTDB_CONTROL_PULL_DB:
196 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_pulldb));
197 return ctdb_control_pull_db(ctdb, indata, outdata);
199 case CTDB_CONTROL_SET_DMASTER:
200 return control_not_implemented("SET_DMASTER", NULL);
202 case CTDB_CONTROL_PUSH_DB:
203 return ctdb_control_push_db(ctdb, indata);
205 case CTDB_CONTROL_GET_RECMODE: {
206 return ctdb->recovery_mode;
209 case CTDB_CONTROL_SET_RECMASTER: {
210 return ctdb_control_set_recmaster(ctdb, opcode, indata);
213 case CTDB_CONTROL_GET_RECMASTER:
214 return ctdb->recovery_master;
216 case CTDB_CONTROL_GET_PID:
217 return getpid();
219 case CTDB_CONTROL_GET_PNN:
220 return ctdb->pnn;
222 case CTDB_CONTROL_PING:
223 CHECK_CONTROL_DATA_SIZE(0);
224 return ctdb->num_clients;
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;
233 case CTDB_CONTROL_SET_DB_READONLY: {
234 uint32_t db_id;
235 struct ctdb_db_context *ctdb_db;
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);
243 case CTDB_CONTROL_GET_DBNAME: {
244 uint32_t db_id;
245 struct ctdb_db_context *ctdb_db;
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;
256 case CTDB_CONTROL_GETDBPATH: {
257 uint32_t db_id;
258 struct ctdb_db_context *ctdb_db;
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;
269 case CTDB_CONTROL_DB_ATTACH:
270 return ctdb_control_db_attach(ctdb, indata, outdata, 0, client_id,
271 c, async_reply);
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);
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);
283 case CTDB_CONTROL_SET_CALL:
284 return control_not_implemented("SET_CALL", NULL);
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);
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);
294 case CTDB_CONTROL_TRAVERSE_ALL:
295 return ctdb_control_traverse_all(ctdb, indata, outdata);
297 case CTDB_CONTROL_TRAVERSE_ALL_EXT:
298 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
300 case CTDB_CONTROL_TRAVERSE_DATA:
301 return ctdb_control_traverse_data(ctdb, indata, outdata);
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);
307 case CTDB_CONTROL_REGISTER_SRVID:
308 return daemon_register_message_handler(ctdb, client_id, srvid);
310 case CTDB_CONTROL_DEREGISTER_SRVID:
311 return daemon_deregister_message_handler(ctdb, client_id, srvid);
313 case CTDB_CONTROL_CHECK_SRVIDS:
314 return control_not_implemented("CHECK_SRVIDS", NULL);
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);
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);
324 case CTDB_CONTROL_FREEZE:
325 CHECK_CONTROL_DATA_SIZE(0);
326 return ctdb_control_freeze(ctdb, c, async_reply);
328 case CTDB_CONTROL_THAW:
329 return control_not_implemented("THAW", NULL);
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);
335 case CTDB_CONTROL_GET_MONMODE:
336 return control_not_implemented("GET_MONMODE", NULL);
338 case CTDB_CONTROL_ENABLE_MONITOR:
339 return control_not_implemented("ENABLE_MONITOR", NULL);
341 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
342 return control_not_implemented("RUN_EVENTSCRIPTS", NULL);
344 case CTDB_CONTROL_DISABLE_MONITOR:
345 return control_not_implemented("DISABLE_MONITOR", NULL);
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;
353 case CTDB_CONTROL_TAKEOVER_IPv4:
354 return control_not_implemented("TAKEOVER_IPv4", "TAKEOVER_IP");
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);
360 case CTDB_CONTROL_RELEASE_IPv4:
361 return control_not_implemented("RELEASE_IPv4", "RELEASE_IP");
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);
367 case CTDB_CONTROL_IPREALLOCATED:
368 CHECK_CONTROL_DATA_SIZE(0);
369 return ctdb_control_ipreallocated(ctdb, c, async_reply);
371 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
372 return control_not_implemented("GET_PUBLIC_IPSv4",
373 "GET_PUBLIC_IPS");
375 case CTDB_CONTROL_GET_PUBLIC_IPS:
376 CHECK_CONTROL_DATA_SIZE(0);
377 return ctdb_control_get_public_ips(ctdb, c, outdata);
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);
383 case CTDB_CONTROL_STARTUP:
384 CHECK_CONTROL_DATA_SIZE(0);
385 return ctdb_control_startup(ctdb, srcnode);
387 case CTDB_CONTROL_TCP_ADD:
388 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
389 return ctdb_control_tcp_add(ctdb, indata, false);
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);
395 case CTDB_CONTROL_TCP_REMOVE:
396 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_connection));
397 return ctdb_control_tcp_remove(ctdb, indata);
399 case CTDB_CONTROL_SET_TUNABLE:
400 return ctdb_control_set_tunable(ctdb, indata);
402 case CTDB_CONTROL_GET_TUNABLE:
403 return ctdb_control_get_tunable(ctdb, indata, outdata);
405 case CTDB_CONTROL_LIST_TUNABLES:
406 return ctdb_control_list_tunables(ctdb, outdata);
408 case CTDB_CONTROL_MODIFY_FLAGS:
409 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
410 return ctdb_control_modflags(ctdb, indata);
412 case CTDB_CONTROL_KILL_TCP:
413 return control_not_implemented("KILL_TCP", NULL);
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);
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);
423 case CTDB_CONTROL_REGISTER_SERVER_ID:
424 return control_not_implemented("REGISTER_SERVER_ID", NULL);
426 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
427 return control_not_implemented("UNREGISTER_SERVER_ID", NULL);
429 case CTDB_CONTROL_CHECK_SERVER_ID:
430 return control_not_implemented("CHECK_SERVER_ID", NULL);
432 case CTDB_CONTROL_GET_SERVER_ID_LIST:
433 return control_not_implemented("SERVER_ID_LIST", NULL);
435 case CTDB_CONTROL_PERSISTENT_STORE:
436 return control_not_implemented("PERSISTENT_STORE", NULL);
438 case CTDB_CONTROL_UPDATE_RECORD:
439 return ctdb_control_update_record(ctdb, c, indata, async_reply);
441 case CTDB_CONTROL_SEND_GRATUITOUS_ARP:
442 return ctdb_control_send_gratious_arp(ctdb, indata);
444 case CTDB_CONTROL_TRANSACTION_START:
445 return control_not_implemented("TRANSACTION_START", NULL);
447 case CTDB_CONTROL_TRANSACTION_COMMIT:
448 return control_not_implemented("TRANSACTION_COMMIT", NULL);
450 case CTDB_CONTROL_WIPE_DATABASE:
451 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_transdb));
452 return ctdb_control_wipe_database(ctdb, indata);
454 case CTDB_CONTROL_UPTIME:
455 return ctdb_control_uptime(ctdb, outdata);
457 case CTDB_CONTROL_START_RECOVERY:
458 return ctdb_control_start_recovery(ctdb, c, async_reply);
460 case CTDB_CONTROL_END_RECOVERY:
461 return ctdb_control_end_recovery(ctdb, c, async_reply);
463 case CTDB_CONTROL_TRY_DELETE_RECORDS:
464 return ctdb_control_try_delete_records(ctdb, indata, outdata);
466 case CTDB_CONTROL_ADD_PUBLIC_IP:
467 return ctdb_control_add_public_address(ctdb, indata);
469 case CTDB_CONTROL_DEL_PUBLIC_IP:
470 return ctdb_control_del_public_address(ctdb, indata);
472 case CTDB_CONTROL_GET_CAPABILITIES:
473 return ctdb_control_get_capabilities(ctdb, outdata);
475 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
476 return ctdb_control_start_persistent_update(ctdb, c, indata);
478 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
479 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
481 case CTDB_CONTROL_TRANS2_COMMIT:
482 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
483 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
485 case CTDB_CONTROL_TRANS2_ERROR:
486 return control_not_implemented("TRANS2_ERROR", NULL);
488 case CTDB_CONTROL_TRANS2_FINISHED:
489 return control_not_implemented("TRANS2_FINISHED", NULL);
491 case CTDB_CONTROL_TRANS2_ACTIVE:
492 return control_not_implemented("TRANS2_ACTIVE", NULL);
494 case CTDB_CONTROL_TRANS3_COMMIT:
495 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
497 case CTDB_CONTROL_RECD_PING:
498 CHECK_CONTROL_DATA_SIZE(0);
499 return ctdb_control_recd_ping(ctdb);
501 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
502 return control_not_implemented("GET_EVENT_SCRIPT_STATUS", NULL);
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;
514 return 0;
515 case CTDB_CONTROL_SET_RECLOCK_FILE:
516 return control_not_implemented("SET_RECLOCK", NULL);
518 case CTDB_CONTROL_STOP_NODE:
519 CHECK_CONTROL_DATA_SIZE(0);
520 return ctdb_control_stop_node(ctdb);
522 case CTDB_CONTROL_CONTINUE_NODE:
523 CHECK_CONTROL_DATA_SIZE(0);
524 return ctdb_control_continue_node(ctdb);
526 case CTDB_CONTROL_SET_NATGWSTATE:
527 return control_not_implemented("SET_NATGWSTATE", NULL);
529 case CTDB_CONTROL_SET_LMASTERROLE: {
530 uint32_t lmasterrole;
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;
539 return 0;
542 case CTDB_CONTROL_SET_RECMASTERROLE: {
543 uint32_t recmasterrole;
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;
552 return 0;
555 case CTDB_CONTROL_ENABLE_SCRIPT:
556 return control_not_implemented("ENABLE_SCRIPT", NULL);
558 case CTDB_CONTROL_DISABLE_SCRIPT:
559 return control_not_implemented("DISABLE_SCRIPT", NULL);
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);
565 case CTDB_CONTROL_GET_BAN_STATE:
566 CHECK_CONTROL_DATA_SIZE(0);
567 return ctdb_control_get_ban_state(ctdb, outdata);
569 case CTDB_CONTROL_SET_DB_PRIORITY:
570 return control_not_implemented("SET_DB_PRIORITY", NULL);
572 case CTDB_CONTROL_GET_DB_PRIORITY:
573 return control_not_implemented("GET_DB_PRIORITY", NULL);
575 case CTDB_CONTROL_TRANSACTION_CANCEL:
576 return control_not_implemented("TRANSACTION_CANCEL", NULL);
578 case CTDB_CONTROL_REGISTER_NOTIFY:
579 return ctdb_control_register_notify(ctdb, client_id, indata);
581 case CTDB_CONTROL_DEREGISTER_NOTIFY:
582 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
583 return ctdb_control_deregister_notify(ctdb, client_id, indata);
585 case CTDB_CONTROL_GET_LOG:
586 return control_not_implemented("GET_LOG", NULL);
588 case CTDB_CONTROL_CLEAR_LOG:
589 return control_not_implemented("CLEAR_LOG", NULL);
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);
595 case CTDB_CONTROL_DB_SET_HEALTHY:
596 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
597 return ctdb_control_db_set_healthy(ctdb, indata);
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);
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);
607 case CTDB_CONTROL_GET_IFACES:
608 CHECK_CONTROL_DATA_SIZE(0);
609 return ctdb_control_get_ifaces(ctdb, c, outdata);
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);
615 case CTDB_CONTROL_GET_STAT_HISTORY:
616 CHECK_CONTROL_DATA_SIZE(0);
617 return ctdb_control_get_stat_history(ctdb, c, outdata);
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);
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);
632 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
633 CHECK_CONTROL_DATA_SIZE(0);
634 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
636 case CTDB_CONTROL_RECEIVE_RECORDS:
637 return ctdb_control_receive_records(ctdb, indata, outdata);
639 case CTDB_CONTROL_DB_DETACH:
640 return ctdb_control_db_detach(ctdb, indata, client_id);
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);
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);
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);
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);
659 case CTDB_CONTROL_DB_TRANSACTION_CANCEL:
660 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
661 return ctdb_control_db_transaction_cancel(ctdb, indata);
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);
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);
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);
675 case CTDB_CONTROL_DB_OPEN_FLAGS: {
676 uint32_t db_id;
677 struct ctdb_db_context *ctdb_db;
678 int tdb_flags;
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;
687 tdb_flags = tdb_get_flags(ctdb_db->ltdb->tdb);
689 outdata->dptr = talloc_size(outdata, sizeof(tdb_flags));
690 if (outdata->dptr == NULL) {
691 return -1;
694 outdata->dsize = sizeof(tdb_flags);
695 memcpy(outdata->dptr, &tdb_flags, outdata->dsize);
696 return 0;
699 case CTDB_CONTROL_CHECK_PID_SRVID:
700 CHECK_CONTROL_DATA_SIZE((sizeof(pid_t) + sizeof(uint64_t)));
701 return ctdb_control_check_pid_srvid(ctdb, indata);
703 default:
704 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
705 return -1;
710 send a reply for a ctdb control
712 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control_old *c,
713 TDB_DATA *outdata, int32_t status, const char *errormsg)
715 struct ctdb_reply_control_old *r;
716 size_t len;
718 /* some controls send no reply */
719 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
720 return;
723 len = offsetof(struct ctdb_reply_control_old, data) + (outdata?outdata->dsize:0);
724 if (errormsg) {
725 len += strlen(errormsg);
727 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control_old);
728 if (r == NULL) {
729 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
730 return;
733 r->hdr.destnode = c->hdr.srcnode;
734 r->hdr.reqid = c->hdr.reqid;
735 r->status = status;
736 r->datalen = outdata?outdata->dsize:0;
737 if (outdata && outdata->dsize) {
738 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
740 if (errormsg) {
741 r->errorlen = strlen(errormsg);
742 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
745 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
747 talloc_free(r);
751 called when a CTDB_REQ_CONTROL packet comes in
753 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
755 struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr;
756 TDB_DATA data, *outdata;
757 int32_t status;
758 bool async_reply = false;
759 const char *errormsg = NULL;
761 data.dptr = &c->data[0];
762 data.dsize = c->datalen;
764 outdata = talloc_zero(c, TDB_DATA);
766 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
767 &errormsg, &async_reply);
769 if (!async_reply) {
770 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
775 called when a CTDB_REPLY_CONTROL packet comes in
777 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
779 struct ctdb_reply_control_old *c = (struct ctdb_reply_control_old *)hdr;
780 TDB_DATA data;
781 struct ctdb_control_state *state;
782 const char *errormsg = NULL;
784 state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_control_state);
785 if (state == NULL) {
786 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
787 ctdb->pnn, hdr->reqid));
788 return;
791 if (hdr->reqid != state->reqid) {
792 /* we found a record but it was the wrong one */
793 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
794 return;
797 data.dptr = &c->data[0];
798 data.dsize = c->datalen;
799 if (c->errorlen) {
800 errormsg = talloc_strndup(state,
801 (char *)&c->data[c->datalen], c->errorlen);
804 /* make state a child of the packet, so it goes away when the packet
805 is freed. */
806 talloc_steal(hdr, state);
808 state->callback(ctdb, c->status, data, errormsg, state->private_data);
811 static int ctdb_control_destructor(struct ctdb_control_state *state)
813 reqid_remove(state->ctdb->idr, state->reqid);
814 return 0;
818 handle a timeout of a control
820 static void ctdb_control_timeout(struct tevent_context *ev,
821 struct tevent_timer *te,
822 struct timeval t, void *private_data)
824 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
825 TALLOC_CTX *tmp_ctx = talloc_new(ev);
827 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
829 talloc_steal(tmp_ctx, state);
831 state->callback(state->ctdb, -1, tdb_null,
832 "ctdb_control timed out",
833 state->private_data);
834 talloc_free(tmp_ctx);
839 send a control message to a node
841 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
842 uint64_t srvid, uint32_t opcode, uint32_t client_id,
843 uint32_t flags,
844 TDB_DATA data,
845 ctdb_control_callback_fn_t callback,
846 void *private_data)
848 struct ctdb_req_control_old *c;
849 struct ctdb_control_state *state;
850 size_t len;
852 if (ctdb->methods == NULL) {
853 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
854 return -1;
857 if (((destnode == CTDB_BROADCAST_VNNMAP) ||
858 (destnode == CTDB_BROADCAST_ALL) ||
859 (destnode == CTDB_BROADCAST_CONNECTED)) &&
860 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
861 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
862 return -1;
865 if (destnode != CTDB_BROADCAST_VNNMAP &&
866 destnode != CTDB_BROADCAST_ALL &&
867 destnode != CTDB_BROADCAST_CONNECTED &&
868 (!ctdb_validate_pnn(ctdb, destnode) ||
869 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
870 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
871 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
873 return 0;
876 /* the state is made a child of private_data if possible. This means any reply
877 will be discarded if the private_data goes away */
878 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
879 CTDB_NO_MEMORY(ctdb, state);
881 state->reqid = reqid_new(ctdb->idr, state);
882 state->callback = callback;
883 state->private_data = private_data;
884 state->ctdb = ctdb;
885 state->flags = flags;
887 talloc_set_destructor(state, ctdb_control_destructor);
889 len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
890 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
891 struct ctdb_req_control_old);
892 CTDB_NO_MEMORY(ctdb, c);
893 talloc_set_name_const(c, "ctdb_req_control packet");
895 c->hdr.destnode = destnode;
896 c->hdr.reqid = state->reqid;
897 c->opcode = opcode;
898 c->client_id = client_id;
899 c->flags = flags;
900 c->srvid = srvid;
901 c->datalen = data.dsize;
902 if (data.dsize) {
903 memcpy(&c->data[0], data.dptr, data.dsize);
906 ctdb_queue_packet(ctdb, &c->hdr);
908 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
909 talloc_free(state);
910 return 0;
913 if (ctdb->tunable.control_timeout) {
914 tevent_add_timer(ctdb->ev, state,
915 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
916 ctdb_control_timeout, state);
919 talloc_free(c);
920 return 0;