param: remove unused dump_globals
[Samba.git] / ctdb / server / ctdb_control.c
blob1f9e4bc5a06bd5bae87353c0ebae35679ab71466
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 "includes.h"
20 #include "tdb.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "../include/ctdb_private.h"
25 #include "lib/util/dlinklist.h"
26 #include "db_wrap.h"
29 struct ctdb_control_state {
30 struct ctdb_context *ctdb;
31 uint32_t reqid;
32 ctdb_control_callback_fn_t callback;
33 void *private_data;
34 unsigned flags;
39 dump talloc memory hierarchy, returning it as a blob to the client
41 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
43 /* dump to a file, then send the file as a blob */
44 FILE *f;
45 long fsize;
46 f = tmpfile();
47 if (f == NULL) {
48 DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n", strerror(errno)));
49 return -1;
51 talloc_report_full(NULL, f);
52 fsize = ftell(f);
53 if (fsize == -1) {
54 DEBUG(DEBUG_ERR, (__location__ " Unable to get file size - %s\n",
55 strerror(errno)));
56 fclose(f);
57 return -1;
59 rewind(f);
60 outdata->dptr = talloc_size(outdata, fsize);
61 if (outdata->dptr == NULL) {
62 fclose(f);
63 CTDB_NO_MEMORY(ctdb, outdata->dptr);
65 outdata->dsize = fread(outdata->dptr, 1, fsize, f);
66 fclose(f);
67 if (outdata->dsize != fsize) {
68 DEBUG(DEBUG_ERR,(__location__ " Unable to read tmpfile\n"));
69 return -1;
71 return 0;
74 static int32_t control_not_implemented(const char *unsupported,
75 const char *alternate)
77 if (alternate == NULL) {
78 DEBUG(DEBUG_ERR,
79 ("Control %s is not implemented any more\n",
80 unsupported));
81 } else {
82 DEBUG(DEBUG_ERR,
83 ("Control %s is not implemented any more, use %s instead\n",
84 unsupported, alternate));
86 return -1;
90 process a control request
92 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
93 struct ctdb_req_control *c,
94 TDB_DATA indata,
95 TDB_DATA *outdata, uint32_t srcnode,
96 const char **errormsg,
97 bool *async_reply)
99 uint32_t opcode = c->opcode;
100 uint64_t srvid = c->srvid;
101 uint32_t client_id = c->client_id;
103 switch (opcode) {
104 case CTDB_CONTROL_PROCESS_EXISTS: {
105 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
106 return ctdb_control_process_exists(ctdb, *(pid_t *)indata.dptr);
109 case CTDB_CONTROL_SET_DEBUG: {
110 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
111 LogLevel = *(int32_t *)indata.dptr;
112 return 0;
115 case CTDB_CONTROL_GET_DEBUG: {
116 CHECK_CONTROL_DATA_SIZE(0);
117 outdata->dptr = (uint8_t *)&LogLevel;
118 outdata->dsize = sizeof(LogLevel);
119 return 0;
122 case CTDB_CONTROL_STATISTICS: {
123 int i;
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 = 0;
128 for (i=1; i<= NUM_DB_PRIORITIES; i++) {
129 if (ctdb->freeze_mode[i] == CTDB_FREEZE_FROZEN) {
130 ctdb->statistics.frozen = 1;
133 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
134 ctdb->statistics.statistics_current_time = timeval_current();
136 outdata->dptr = (uint8_t *)&ctdb->statistics;
137 outdata->dsize = sizeof(ctdb->statistics);
138 return 0;
141 case CTDB_CONTROL_GET_ALL_TUNABLES: {
142 CHECK_CONTROL_DATA_SIZE(0);
143 outdata->dptr = (uint8_t *)&ctdb->tunable;
144 outdata->dsize = sizeof(ctdb->tunable);
145 return 0;
148 case CTDB_CONTROL_DUMP_MEMORY: {
149 CHECK_CONTROL_DATA_SIZE(0);
150 return ctdb_dump_memory(ctdb, outdata);
153 case CTDB_CONTROL_STATISTICS_RESET: {
154 CHECK_CONTROL_DATA_SIZE(0);
155 ZERO_STRUCT(ctdb->statistics);
156 ctdb->statistics.statistics_start_time = timeval_current();
157 return 0;
160 case CTDB_CONTROL_GETVNNMAP:
161 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
163 case CTDB_CONTROL_GET_DBMAP:
164 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
166 case CTDB_CONTROL_GET_NODEMAPv4:
167 return ctdb_control_getnodemapv4(ctdb, opcode, indata, outdata);
169 case CTDB_CONTROL_GET_NODEMAP:
170 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
172 case CTDB_CONTROL_RELOAD_NODES_FILE:
173 CHECK_CONTROL_DATA_SIZE(0);
174 return ctdb_control_reload_nodes_file(ctdb, opcode);
176 case CTDB_CONTROL_SET_DB_STICKY: {
177 uint32_t db_id;
178 struct ctdb_db_context *ctdb_db;
180 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
181 db_id = *(uint32_t *)indata.dptr;
182 ctdb_db = find_ctdb_db(ctdb, db_id);
183 if (ctdb_db == NULL) return -1;
184 return ctdb_set_db_sticky(ctdb, ctdb_db);
187 case CTDB_CONTROL_SETVNNMAP:
188 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
190 case CTDB_CONTROL_PULL_DB:
191 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_pulldb));
192 return ctdb_control_pull_db(ctdb, indata, outdata);
194 case CTDB_CONTROL_SET_DMASTER:
195 return control_not_implemented("SET_DMASTER", NULL);
197 case CTDB_CONTROL_PUSH_DB:
198 return ctdb_control_push_db(ctdb, indata);
200 case CTDB_CONTROL_GET_RECMODE: {
201 return ctdb->recovery_mode;
204 case CTDB_CONTROL_SET_RECMASTER: {
205 return ctdb_control_set_recmaster(ctdb, opcode, indata);
208 case CTDB_CONTROL_GET_RECMASTER:
209 return ctdb->recovery_master;
211 case CTDB_CONTROL_GET_PID:
212 return getpid();
214 case CTDB_CONTROL_GET_PNN:
215 return ctdb->pnn;
217 case CTDB_CONTROL_PING:
218 CHECK_CONTROL_DATA_SIZE(0);
219 return ctdb->num_clients;
221 case CTDB_CONTROL_GET_RUNSTATE:
222 CHECK_CONTROL_DATA_SIZE(0);
223 outdata->dptr = (uint8_t *)&ctdb->runstate;
224 outdata->dsize = sizeof(uint32_t);
225 return 0;
228 case CTDB_CONTROL_SET_DB_READONLY: {
229 uint32_t db_id;
230 struct ctdb_db_context *ctdb_db;
232 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
233 db_id = *(uint32_t *)indata.dptr;
234 ctdb_db = find_ctdb_db(ctdb, db_id);
235 if (ctdb_db == NULL) return -1;
236 return ctdb_set_db_readonly(ctdb, ctdb_db);
238 case CTDB_CONTROL_GET_DBNAME: {
239 uint32_t db_id;
240 struct ctdb_db_context *ctdb_db;
242 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
243 db_id = *(uint32_t *)indata.dptr;
244 ctdb_db = find_ctdb_db(ctdb, db_id);
245 if (ctdb_db == NULL) return -1;
246 outdata->dptr = discard_const(ctdb_db->db_name);
247 outdata->dsize = strlen(ctdb_db->db_name)+1;
248 return 0;
251 case CTDB_CONTROL_GETDBPATH: {
252 uint32_t db_id;
253 struct ctdb_db_context *ctdb_db;
255 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
256 db_id = *(uint32_t *)indata.dptr;
257 ctdb_db = find_ctdb_db(ctdb, db_id);
258 if (ctdb_db == NULL) return -1;
259 outdata->dptr = discard_const(ctdb_db->db_path);
260 outdata->dsize = strlen(ctdb_db->db_path)+1;
261 return 0;
264 case CTDB_CONTROL_DB_ATTACH:
265 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, false, client_id, c, async_reply);
267 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
268 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, true, client_id, c, async_reply);
270 case CTDB_CONTROL_SET_CALL: {
271 struct ctdb_control_set_call *sc =
272 (struct ctdb_control_set_call *)indata.dptr;
273 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_call));
274 return ctdb_daemon_set_call(ctdb, sc->db_id, sc->fn, sc->id);
277 case CTDB_CONTROL_TRAVERSE_START:
278 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
279 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
281 case CTDB_CONTROL_TRAVERSE_START_EXT:
282 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start_ext));
283 return ctdb_control_traverse_start_ext(ctdb, indata, outdata, srcnode, client_id);
285 case CTDB_CONTROL_TRAVERSE_ALL:
286 return ctdb_control_traverse_all(ctdb, indata, outdata);
288 case CTDB_CONTROL_TRAVERSE_ALL_EXT:
289 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
291 case CTDB_CONTROL_TRAVERSE_DATA:
292 return ctdb_control_traverse_data(ctdb, indata, outdata);
294 case CTDB_CONTROL_TRAVERSE_KILL:
295 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
296 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
298 case CTDB_CONTROL_REGISTER_SRVID:
299 return daemon_register_message_handler(ctdb, client_id, srvid);
301 case CTDB_CONTROL_DEREGISTER_SRVID:
302 return daemon_deregister_message_handler(ctdb, client_id, srvid);
304 case CTDB_CONTROL_CHECK_SRVIDS:
305 return daemon_check_srvids(ctdb, indata, outdata);
307 case CTDB_CONTROL_ENABLE_SEQNUM:
308 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
309 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
311 case CTDB_CONTROL_UPDATE_SEQNUM:
312 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
313 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
315 case CTDB_CONTROL_FREEZE:
316 CHECK_CONTROL_DATA_SIZE(0);
317 return ctdb_control_freeze(ctdb, c, async_reply);
319 case CTDB_CONTROL_THAW:
320 CHECK_CONTROL_DATA_SIZE(0);
321 return ctdb_control_thaw(ctdb, (uint32_t)c->srvid, true);
323 case CTDB_CONTROL_SET_RECMODE:
324 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
325 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
327 case CTDB_CONTROL_GET_MONMODE:
328 CHECK_CONTROL_DATA_SIZE(0);
329 return ctdb_monitoring_mode(ctdb);
331 case CTDB_CONTROL_ENABLE_MONITOR:
332 CHECK_CONTROL_DATA_SIZE(0);
333 ctdb_enable_monitoring(ctdb);
334 return 0;
336 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
337 return ctdb_run_eventscripts(ctdb, c, indata, async_reply);
339 case CTDB_CONTROL_DISABLE_MONITOR:
340 CHECK_CONTROL_DATA_SIZE(0);
341 ctdb_disable_monitoring(ctdb);
342 return 0;
344 case CTDB_CONTROL_SHUTDOWN:
345 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
346 ctdb_shutdown_sequence(ctdb, 0);
347 /* In case above returns due to duplicate shutdown */
348 return 0;
350 case CTDB_CONTROL_TAKEOVER_IPv4:
351 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
352 return ctdb_control_takeover_ipv4(ctdb, c, indata, async_reply);
354 case CTDB_CONTROL_TAKEOVER_IP:
355 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
356 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
358 case CTDB_CONTROL_RELEASE_IPv4:
359 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
360 return ctdb_control_release_ipv4(ctdb, c, indata, async_reply);
362 case CTDB_CONTROL_RELEASE_IP:
363 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
364 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
366 case CTDB_CONTROL_IPREALLOCATED:
367 CHECK_CONTROL_DATA_SIZE(0);
368 return ctdb_control_ipreallocated(ctdb, c, async_reply);
370 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
371 CHECK_CONTROL_DATA_SIZE(0);
372 return ctdb_control_get_public_ipsv4(ctdb, c, outdata);
374 case CTDB_CONTROL_GET_PUBLIC_IPS:
375 CHECK_CONTROL_DATA_SIZE(0);
376 return ctdb_control_get_public_ips(ctdb, c, outdata);
378 case CTDB_CONTROL_TCP_CLIENT:
379 return ctdb_control_tcp_client(ctdb, client_id, indata);
381 case CTDB_CONTROL_STARTUP:
382 CHECK_CONTROL_DATA_SIZE(0);
383 return ctdb_control_startup(ctdb, srcnode);
385 case CTDB_CONTROL_TCP_ADD:
386 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
387 return ctdb_control_tcp_add(ctdb, indata, false);
389 case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
390 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
391 return ctdb_control_tcp_add(ctdb, indata, true);
393 case CTDB_CONTROL_TCP_REMOVE:
394 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
395 return ctdb_control_tcp_remove(ctdb, indata);
397 case CTDB_CONTROL_SET_TUNABLE:
398 return ctdb_control_set_tunable(ctdb, indata);
400 case CTDB_CONTROL_GET_TUNABLE:
401 return ctdb_control_get_tunable(ctdb, indata, outdata);
403 case CTDB_CONTROL_LIST_TUNABLES:
404 return ctdb_control_list_tunables(ctdb, outdata);
406 case CTDB_CONTROL_MODIFY_FLAGS:
407 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
408 return ctdb_control_modflags(ctdb, indata);
410 case CTDB_CONTROL_KILL_TCP:
411 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_killtcp));
412 return ctdb_control_kill_tcp(ctdb, indata);
414 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
415 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
416 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
418 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
419 /* data size is verified in the called function */
420 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
422 case CTDB_CONTROL_REGISTER_SERVER_ID:
423 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
424 return ctdb_control_register_server_id(ctdb, client_id, indata);
426 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
427 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
428 return ctdb_control_unregister_server_id(ctdb, indata);
430 case CTDB_CONTROL_CHECK_SERVER_ID:
431 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
432 return ctdb_control_check_server_id(ctdb, indata);
434 case CTDB_CONTROL_GET_SERVER_ID_LIST:
435 CHECK_CONTROL_DATA_SIZE(0);
436 return ctdb_control_get_server_id_list(ctdb, outdata);
438 case CTDB_CONTROL_PERSISTENT_STORE:
439 return control_not_implemented("PERSISTENT_STORE", NULL);
441 case CTDB_CONTROL_UPDATE_RECORD:
442 return ctdb_control_update_record(ctdb, c, indata, async_reply);
444 case CTDB_CONTROL_SEND_GRATIOUS_ARP:
445 return ctdb_control_send_gratious_arp(ctdb, indata);
447 case CTDB_CONTROL_TRANSACTION_START:
448 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
449 return ctdb_control_transaction_start(ctdb, *(uint32_t *)indata.dptr);
451 case CTDB_CONTROL_TRANSACTION_COMMIT:
452 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
453 return ctdb_control_transaction_commit(ctdb, *(uint32_t *)indata.dptr);
455 case CTDB_CONTROL_WIPE_DATABASE:
456 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_wipe_database));
457 return ctdb_control_wipe_database(ctdb, indata);
459 case CTDB_CONTROL_UPTIME:
460 return ctdb_control_uptime(ctdb, outdata);
462 case CTDB_CONTROL_START_RECOVERY:
463 return ctdb_control_start_recovery(ctdb, c, async_reply);
465 case CTDB_CONTROL_END_RECOVERY:
466 return ctdb_control_end_recovery(ctdb, c, async_reply);
468 case CTDB_CONTROL_TRY_DELETE_RECORDS:
469 return ctdb_control_try_delete_records(ctdb, indata, outdata);
471 case CTDB_CONTROL_ADD_PUBLIC_IP:
472 return ctdb_control_add_public_address(ctdb, indata);
474 case CTDB_CONTROL_DEL_PUBLIC_IP:
475 return ctdb_control_del_public_address(ctdb, c, indata,
476 async_reply);
478 case CTDB_CONTROL_GET_CAPABILITIES:
479 return ctdb_control_get_capabilities(ctdb, outdata);
481 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
482 return ctdb_control_start_persistent_update(ctdb, c, indata);
484 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
485 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
487 case CTDB_CONTROL_TRANS2_COMMIT:
488 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
489 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
491 case CTDB_CONTROL_TRANS2_ERROR:
492 return control_not_implemented("TRANS2_ERROR", NULL);
494 case CTDB_CONTROL_TRANS2_FINISHED:
495 return control_not_implemented("TRANS2_FINISHED", NULL);
497 case CTDB_CONTROL_TRANS2_ACTIVE:
498 return control_not_implemented("TRANS2_ACTIVE", NULL);
500 case CTDB_CONTROL_TRANS3_COMMIT:
501 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
503 case CTDB_CONTROL_RECD_PING:
504 CHECK_CONTROL_DATA_SIZE(0);
505 return ctdb_control_recd_ping(ctdb);
507 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
508 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
509 return ctdb_control_get_event_script_status(ctdb, *(uint32_t *)indata.dptr, outdata);
511 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
512 CHECK_CONTROL_DATA_SIZE(sizeof(double));
513 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
514 return 0;
515 case CTDB_CONTROL_GET_RECLOCK_FILE:
516 CHECK_CONTROL_DATA_SIZE(0);
517 if (ctdb->recovery_lock_file != NULL) {
518 outdata->dptr = discard_const(ctdb->recovery_lock_file);
519 outdata->dsize = strlen(ctdb->recovery_lock_file) + 1;
521 return 0;
522 case CTDB_CONTROL_SET_RECLOCK_FILE:
523 ctdb->tunable.verify_recovery_lock = 0;
524 if (ctdb->recovery_lock_file != NULL) {
525 talloc_free(ctdb->recovery_lock_file);
526 ctdb->recovery_lock_file = NULL;
528 if (indata.dsize > 0) {
529 ctdb->recovery_lock_file = talloc_strdup(ctdb, discard_const(indata.dptr));
530 ctdb->tunable.verify_recovery_lock = 1;
532 return 0;
534 case CTDB_CONTROL_STOP_NODE:
535 CHECK_CONTROL_DATA_SIZE(0);
536 return ctdb_control_stop_node(ctdb);
538 case CTDB_CONTROL_CONTINUE_NODE:
539 CHECK_CONTROL_DATA_SIZE(0);
540 return ctdb_control_continue_node(ctdb);
542 case CTDB_CONTROL_SET_NATGWSTATE: {
543 uint32_t natgwstate;
545 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
546 natgwstate = *(uint32_t *)indata.dptr;
547 if (natgwstate == 0) {
548 ctdb->capabilities &= ~CTDB_CAP_NATGW;
549 } else {
550 ctdb->capabilities |= CTDB_CAP_NATGW;
552 return 0;
555 case CTDB_CONTROL_SET_LMASTERROLE: {
556 uint32_t lmasterrole;
558 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
559 lmasterrole = *(uint32_t *)indata.dptr;
560 if (lmasterrole == 0) {
561 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
562 } else {
563 ctdb->capabilities |= CTDB_CAP_LMASTER;
565 return 0;
568 case CTDB_CONTROL_SET_RECMASTERROLE: {
569 uint32_t recmasterrole;
571 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
572 recmasterrole = *(uint32_t *)indata.dptr;
573 if (recmasterrole == 0) {
574 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
575 } else {
576 ctdb->capabilities |= CTDB_CAP_RECMASTER;
578 return 0;
581 case CTDB_CONTROL_ENABLE_SCRIPT:
582 return ctdb_control_enable_script(ctdb, indata);
584 case CTDB_CONTROL_DISABLE_SCRIPT:
585 return ctdb_control_disable_script(ctdb, indata);
587 case CTDB_CONTROL_SET_BAN_STATE:
588 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_time));
589 return ctdb_control_set_ban_state(ctdb, indata);
591 case CTDB_CONTROL_GET_BAN_STATE:
592 CHECK_CONTROL_DATA_SIZE(0);
593 return ctdb_control_get_ban_state(ctdb, outdata);
595 case CTDB_CONTROL_SET_DB_PRIORITY:
596 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_db_priority));
597 return ctdb_control_set_db_priority(ctdb, indata, client_id);
599 case CTDB_CONTROL_GET_DB_PRIORITY: {
600 uint32_t db_id;
601 struct ctdb_db_context *ctdb_db;
603 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
604 db_id = *(uint32_t *)indata.dptr;
605 ctdb_db = find_ctdb_db(ctdb, db_id);
606 if (ctdb_db == NULL) return -1;
607 return ctdb_db->priority;
610 case CTDB_CONTROL_TRANSACTION_CANCEL:
611 CHECK_CONTROL_DATA_SIZE(0);
612 return ctdb_control_transaction_cancel(ctdb);
614 case CTDB_CONTROL_REGISTER_NOTIFY:
615 return ctdb_control_register_notify(ctdb, client_id, indata);
617 case CTDB_CONTROL_DEREGISTER_NOTIFY:
618 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_client_notify_deregister));
619 return ctdb_control_deregister_notify(ctdb, client_id, indata);
621 case CTDB_CONTROL_GET_LOG:
622 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_get_log_addr));
623 return ctdb_control_get_log(ctdb, indata);
625 case CTDB_CONTROL_CLEAR_LOG:
626 return ctdb_control_clear_log(ctdb);
628 case CTDB_CONTROL_GET_DB_SEQNUM:
629 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
630 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
632 case CTDB_CONTROL_DB_SET_HEALTHY:
633 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
634 return ctdb_control_db_set_healthy(ctdb, indata);
636 case CTDB_CONTROL_DB_GET_HEALTH:
637 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
638 return ctdb_control_db_get_health(ctdb, indata, outdata);
640 case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
641 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
642 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
644 case CTDB_CONTROL_GET_IFACES:
645 CHECK_CONTROL_DATA_SIZE(0);
646 return ctdb_control_get_ifaces(ctdb, c, outdata);
648 case CTDB_CONTROL_SET_IFACE_LINK_STATE:
649 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_iface_info));
650 return ctdb_control_set_iface_link(ctdb, c, indata);
652 case CTDB_CONTROL_GET_STAT_HISTORY:
653 CHECK_CONTROL_DATA_SIZE(0);
654 return ctdb_control_get_stat_history(ctdb, c, outdata);
656 case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
657 struct ctdb_control_schedule_for_deletion *d;
658 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
659 CHECK_CONTROL_MIN_DATA_SIZE(size);
660 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
661 size += d->keylen;
662 CHECK_CONTROL_DATA_SIZE(size);
663 return ctdb_control_schedule_for_deletion(ctdb, indata);
665 case CTDB_CONTROL_GET_DB_STATISTICS:
666 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
667 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
669 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
670 CHECK_CONTROL_DATA_SIZE(0);
671 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
673 case CTDB_CONTROL_RECEIVE_RECORDS:
674 return ctdb_control_receive_records(ctdb, indata, outdata);
676 case CTDB_CONTROL_DB_DETACH:
677 return ctdb_control_db_detach(ctdb, indata, client_id);
679 default:
680 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
681 return -1;
686 send a reply for a ctdb control
688 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
689 TDB_DATA *outdata, int32_t status, const char *errormsg)
691 struct ctdb_reply_control *r;
692 size_t len;
694 /* some controls send no reply */
695 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
696 return;
699 len = offsetof(struct ctdb_reply_control, data) + (outdata?outdata->dsize:0);
700 if (errormsg) {
701 len += strlen(errormsg);
703 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control);
704 if (r == NULL) {
705 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
706 return;
709 r->hdr.destnode = c->hdr.srcnode;
710 r->hdr.reqid = c->hdr.reqid;
711 r->status = status;
712 r->datalen = outdata?outdata->dsize:0;
713 if (outdata && outdata->dsize) {
714 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
716 if (errormsg) {
717 r->errorlen = strlen(errormsg);
718 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
721 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
723 talloc_free(r);
727 called when a CTDB_REQ_CONTROL packet comes in
729 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
731 struct ctdb_req_control *c = (struct ctdb_req_control *)hdr;
732 TDB_DATA data, *outdata;
733 int32_t status;
734 bool async_reply = false;
735 const char *errormsg = NULL;
737 data.dptr = &c->data[0];
738 data.dsize = c->datalen;
740 outdata = talloc_zero(c, TDB_DATA);
742 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
743 &errormsg, &async_reply);
745 if (!async_reply) {
746 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
751 called when a CTDB_REPLY_CONTROL packet comes in
753 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
755 struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
756 TDB_DATA data;
757 struct ctdb_control_state *state;
758 const char *errormsg = NULL;
760 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_control_state);
761 if (state == NULL) {
762 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
763 ctdb->pnn, hdr->reqid));
764 return;
767 if (hdr->reqid != state->reqid) {
768 /* we found a record but it was the wrong one */
769 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
770 return;
773 data.dptr = &c->data[0];
774 data.dsize = c->datalen;
775 if (c->errorlen) {
776 errormsg = talloc_strndup(state,
777 (char *)&c->data[c->datalen], c->errorlen);
780 /* make state a child of the packet, so it goes away when the packet
781 is freed. */
782 talloc_steal(hdr, state);
784 state->callback(ctdb, c->status, data, errormsg, state->private_data);
787 static int ctdb_control_destructor(struct ctdb_control_state *state)
789 ctdb_reqid_remove(state->ctdb, state->reqid);
790 return 0;
794 handle a timeout of a control
796 static void ctdb_control_timeout(struct event_context *ev, struct timed_event *te,
797 struct timeval t, void *private_data)
799 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
800 TALLOC_CTX *tmp_ctx = talloc_new(ev);
802 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
804 talloc_steal(tmp_ctx, state);
806 state->callback(state->ctdb, -1, tdb_null,
807 "ctdb_control timed out",
808 state->private_data);
809 talloc_free(tmp_ctx);
814 send a control message to a node
816 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
817 uint64_t srvid, uint32_t opcode, uint32_t client_id,
818 uint32_t flags,
819 TDB_DATA data,
820 ctdb_control_callback_fn_t callback,
821 void *private_data)
823 struct ctdb_req_control *c;
824 struct ctdb_control_state *state;
825 size_t len;
827 if (ctdb->methods == NULL) {
828 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
829 return -1;
832 if (((destnode == CTDB_BROADCAST_VNNMAP) ||
833 (destnode == CTDB_BROADCAST_ALL) ||
834 (destnode == CTDB_BROADCAST_CONNECTED)) &&
835 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
836 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
837 return -1;
840 if (destnode != CTDB_BROADCAST_VNNMAP &&
841 destnode != CTDB_BROADCAST_ALL &&
842 destnode != CTDB_BROADCAST_CONNECTED &&
843 (!ctdb_validate_pnn(ctdb, destnode) ||
844 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
845 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
846 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
848 return 0;
851 /* the state is made a child of private_data if possible. This means any reply
852 will be discarded if the private_data goes away */
853 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
854 CTDB_NO_MEMORY(ctdb, state);
856 state->reqid = ctdb_reqid_new(ctdb, state);
857 state->callback = callback;
858 state->private_data = private_data;
859 state->ctdb = ctdb;
860 state->flags = flags;
862 talloc_set_destructor(state, ctdb_control_destructor);
864 len = offsetof(struct ctdb_req_control, data) + data.dsize;
865 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
866 struct ctdb_req_control);
867 CTDB_NO_MEMORY(ctdb, c);
868 talloc_set_name_const(c, "ctdb_req_control packet");
870 c->hdr.destnode = destnode;
871 c->hdr.reqid = state->reqid;
872 c->opcode = opcode;
873 c->client_id = client_id;
874 c->flags = flags;
875 c->srvid = srvid;
876 c->datalen = data.dsize;
877 if (data.dsize) {
878 memcpy(&c->data[0], data.dptr, data.dsize);
881 ctdb_queue_packet(ctdb, &c->hdr);
883 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
884 talloc_free(state);
885 return 0;
888 if (ctdb->tunable.control_timeout) {
889 event_add_timed(ctdb->ev, state,
890 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
891 ctdb_control_timeout, state);
894 talloc_free(c);
895 return 0;