dsdb: collapse wrong password and no-password-hash errors into one handler
[Samba/wip.git] / ctdb / server / ctdb_control.c
blobb407a1ff7ae1ed2cad5033b3b7cfb19d14085f3c
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 int i;
202 if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE) {
203 return CTDB_RECOVERY_ACTIVE;
205 for (i=1; i<=NUM_DB_PRIORITIES; i++) {
206 if (ctdb->freeze_mode[i] == CTDB_FREEZE_FROZEN) {
207 return CTDB_RECOVERY_ACTIVE;
210 return CTDB_RECOVERY_NORMAL;
213 case CTDB_CONTROL_SET_RECMASTER: {
214 return ctdb_control_set_recmaster(ctdb, opcode, indata);
217 case CTDB_CONTROL_GET_RECMASTER:
218 return ctdb->recovery_master;
220 case CTDB_CONTROL_GET_PID:
221 return getpid();
223 case CTDB_CONTROL_GET_PNN:
224 return ctdb->pnn;
226 case CTDB_CONTROL_PING:
227 CHECK_CONTROL_DATA_SIZE(0);
228 return ctdb->num_clients;
230 case CTDB_CONTROL_GET_RUNSTATE:
231 CHECK_CONTROL_DATA_SIZE(0);
232 outdata->dptr = (uint8_t *)&ctdb->runstate;
233 outdata->dsize = sizeof(uint32_t);
234 return 0;
237 case CTDB_CONTROL_SET_DB_READONLY: {
238 uint32_t db_id;
239 struct ctdb_db_context *ctdb_db;
241 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
242 db_id = *(uint32_t *)indata.dptr;
243 ctdb_db = find_ctdb_db(ctdb, db_id);
244 if (ctdb_db == NULL) return -1;
245 return ctdb_set_db_readonly(ctdb, ctdb_db);
247 case CTDB_CONTROL_GET_DBNAME: {
248 uint32_t db_id;
249 struct ctdb_db_context *ctdb_db;
251 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
252 db_id = *(uint32_t *)indata.dptr;
253 ctdb_db = find_ctdb_db(ctdb, db_id);
254 if (ctdb_db == NULL) return -1;
255 outdata->dptr = discard_const(ctdb_db->db_name);
256 outdata->dsize = strlen(ctdb_db->db_name)+1;
257 return 0;
260 case CTDB_CONTROL_GETDBPATH: {
261 uint32_t db_id;
262 struct ctdb_db_context *ctdb_db;
264 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
265 db_id = *(uint32_t *)indata.dptr;
266 ctdb_db = find_ctdb_db(ctdb, db_id);
267 if (ctdb_db == NULL) return -1;
268 outdata->dptr = discard_const(ctdb_db->db_path);
269 outdata->dsize = strlen(ctdb_db->db_path)+1;
270 return 0;
273 case CTDB_CONTROL_DB_ATTACH:
274 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, false, client_id, c, async_reply);
276 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
277 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, true, client_id, c, async_reply);
279 case CTDB_CONTROL_SET_CALL: {
280 struct ctdb_control_set_call *sc =
281 (struct ctdb_control_set_call *)indata.dptr;
282 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_call));
283 return ctdb_daemon_set_call(ctdb, sc->db_id, sc->fn, sc->id);
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 daemon_check_srvids(ctdb, indata, outdata);
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 CHECK_CONTROL_DATA_SIZE(0);
330 return ctdb_control_thaw(ctdb, (uint32_t)c->srvid);
332 case CTDB_CONTROL_SET_RECMODE:
333 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
334 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
336 case CTDB_CONTROL_GET_MONMODE:
337 CHECK_CONTROL_DATA_SIZE(0);
338 return ctdb_monitoring_mode(ctdb);
340 case CTDB_CONTROL_ENABLE_MONITOR:
341 CHECK_CONTROL_DATA_SIZE(0);
342 ctdb_enable_monitoring(ctdb);
343 return 0;
345 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
346 return ctdb_run_eventscripts(ctdb, c, indata, async_reply);
348 case CTDB_CONTROL_DISABLE_MONITOR:
349 CHECK_CONTROL_DATA_SIZE(0);
350 ctdb_disable_monitoring(ctdb);
351 return 0;
353 case CTDB_CONTROL_SHUTDOWN:
354 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
355 ctdb_shutdown_sequence(ctdb, 0);
356 /* In case above returns due to duplicate shutdown */
357 return 0;
359 case CTDB_CONTROL_TAKEOVER_IPv4:
360 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
361 return ctdb_control_takeover_ipv4(ctdb, c, indata, async_reply);
363 case CTDB_CONTROL_TAKEOVER_IP:
364 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
365 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
367 case CTDB_CONTROL_RELEASE_IPv4:
368 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
369 return ctdb_control_release_ipv4(ctdb, c, indata, async_reply);
371 case CTDB_CONTROL_RELEASE_IP:
372 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
373 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
375 case CTDB_CONTROL_IPREALLOCATED:
376 CHECK_CONTROL_DATA_SIZE(0);
377 return ctdb_control_ipreallocated(ctdb, c, async_reply);
379 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
380 CHECK_CONTROL_DATA_SIZE(0);
381 return ctdb_control_get_public_ipsv4(ctdb, c, outdata);
383 case CTDB_CONTROL_GET_PUBLIC_IPS:
384 CHECK_CONTROL_DATA_SIZE(0);
385 return ctdb_control_get_public_ips(ctdb, c, outdata);
387 case CTDB_CONTROL_TCP_CLIENT:
388 return ctdb_control_tcp_client(ctdb, client_id, indata);
390 case CTDB_CONTROL_STARTUP:
391 CHECK_CONTROL_DATA_SIZE(0);
392 return ctdb_control_startup(ctdb, srcnode);
394 case CTDB_CONTROL_TCP_ADD:
395 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
396 return ctdb_control_tcp_add(ctdb, indata, false);
398 case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
399 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
400 return ctdb_control_tcp_add(ctdb, indata, true);
402 case CTDB_CONTROL_TCP_REMOVE:
403 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
404 return ctdb_control_tcp_remove(ctdb, indata);
406 case CTDB_CONTROL_SET_TUNABLE:
407 return ctdb_control_set_tunable(ctdb, indata);
409 case CTDB_CONTROL_GET_TUNABLE:
410 return ctdb_control_get_tunable(ctdb, indata, outdata);
412 case CTDB_CONTROL_LIST_TUNABLES:
413 return ctdb_control_list_tunables(ctdb, outdata);
415 case CTDB_CONTROL_MODIFY_FLAGS:
416 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
417 return ctdb_control_modflags(ctdb, indata);
419 case CTDB_CONTROL_KILL_TCP:
420 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_killtcp));
421 return ctdb_control_kill_tcp(ctdb, indata);
423 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
424 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
425 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
427 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
428 /* data size is verified in the called function */
429 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
431 case CTDB_CONTROL_REGISTER_SERVER_ID:
432 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
433 return ctdb_control_register_server_id(ctdb, client_id, indata);
435 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
436 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
437 return ctdb_control_unregister_server_id(ctdb, indata);
439 case CTDB_CONTROL_CHECK_SERVER_ID:
440 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
441 return ctdb_control_check_server_id(ctdb, indata);
443 case CTDB_CONTROL_GET_SERVER_ID_LIST:
444 CHECK_CONTROL_DATA_SIZE(0);
445 return ctdb_control_get_server_id_list(ctdb, outdata);
447 case CTDB_CONTROL_PERSISTENT_STORE:
448 return control_not_implemented("PERSISTENT_STORE", NULL);
450 case CTDB_CONTROL_UPDATE_RECORD:
451 return ctdb_control_update_record(ctdb, c, indata, async_reply);
453 case CTDB_CONTROL_SEND_GRATIOUS_ARP:
454 return ctdb_control_send_gratious_arp(ctdb, indata);
456 case CTDB_CONTROL_TRANSACTION_START:
457 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
458 return ctdb_control_transaction_start(ctdb, *(uint32_t *)indata.dptr);
460 case CTDB_CONTROL_TRANSACTION_COMMIT:
461 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
462 return ctdb_control_transaction_commit(ctdb, *(uint32_t *)indata.dptr);
464 case CTDB_CONTROL_WIPE_DATABASE:
465 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_wipe_database));
466 return ctdb_control_wipe_database(ctdb, indata);
468 case CTDB_CONTROL_UPTIME:
469 return ctdb_control_uptime(ctdb, outdata);
471 case CTDB_CONTROL_START_RECOVERY:
472 return ctdb_control_start_recovery(ctdb, c, async_reply);
474 case CTDB_CONTROL_END_RECOVERY:
475 return ctdb_control_end_recovery(ctdb, c, async_reply);
477 case CTDB_CONTROL_TRY_DELETE_RECORDS:
478 return ctdb_control_try_delete_records(ctdb, indata, outdata);
480 case CTDB_CONTROL_ADD_PUBLIC_IP:
481 return ctdb_control_add_public_address(ctdb, indata);
483 case CTDB_CONTROL_DEL_PUBLIC_IP:
484 return ctdb_control_del_public_address(ctdb, c, indata,
485 async_reply);
487 case CTDB_CONTROL_GET_CAPABILITIES:
488 return ctdb_control_get_capabilities(ctdb, outdata);
490 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
491 return ctdb_control_start_persistent_update(ctdb, c, indata);
493 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
494 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
496 case CTDB_CONTROL_TRANS2_COMMIT:
497 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
498 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
500 case CTDB_CONTROL_TRANS2_ERROR:
501 return control_not_implemented("TRANS2_ERROR", NULL);
503 case CTDB_CONTROL_TRANS2_FINISHED:
504 return control_not_implemented("TRANS2_FINISHED", NULL);
506 case CTDB_CONTROL_TRANS2_ACTIVE:
507 return control_not_implemented("TRANS2_ACTIVE", NULL);
509 case CTDB_CONTROL_TRANS3_COMMIT:
510 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
512 case CTDB_CONTROL_RECD_PING:
513 CHECK_CONTROL_DATA_SIZE(0);
514 return ctdb_control_recd_ping(ctdb);
516 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
517 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
518 return ctdb_control_get_event_script_status(ctdb, *(uint32_t *)indata.dptr, outdata);
520 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
521 CHECK_CONTROL_DATA_SIZE(sizeof(double));
522 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
523 return 0;
524 case CTDB_CONTROL_GET_RECLOCK_FILE:
525 CHECK_CONTROL_DATA_SIZE(0);
526 if (ctdb->recovery_lock_file != NULL) {
527 outdata->dptr = discard_const(ctdb->recovery_lock_file);
528 outdata->dsize = strlen(ctdb->recovery_lock_file) + 1;
530 return 0;
531 case CTDB_CONTROL_SET_RECLOCK_FILE:
532 ctdb->tunable.verify_recovery_lock = 0;
533 if (ctdb->recovery_lock_file != NULL) {
534 talloc_free(ctdb->recovery_lock_file);
535 ctdb->recovery_lock_file = NULL;
537 if (indata.dsize > 0) {
538 ctdb->recovery_lock_file = talloc_strdup(ctdb, discard_const(indata.dptr));
539 ctdb->tunable.verify_recovery_lock = 1;
541 return 0;
543 case CTDB_CONTROL_STOP_NODE:
544 CHECK_CONTROL_DATA_SIZE(0);
545 return ctdb_control_stop_node(ctdb);
547 case CTDB_CONTROL_CONTINUE_NODE:
548 CHECK_CONTROL_DATA_SIZE(0);
549 return ctdb_control_continue_node(ctdb);
551 case CTDB_CONTROL_SET_NATGWSTATE: {
552 uint32_t natgwstate;
554 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
555 natgwstate = *(uint32_t *)indata.dptr;
556 if (natgwstate == 0) {
557 ctdb->capabilities &= ~CTDB_CAP_NATGW;
558 } else {
559 ctdb->capabilities |= CTDB_CAP_NATGW;
561 return 0;
564 case CTDB_CONTROL_SET_LMASTERROLE: {
565 uint32_t lmasterrole;
567 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
568 lmasterrole = *(uint32_t *)indata.dptr;
569 if (lmasterrole == 0) {
570 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
571 } else {
572 ctdb->capabilities |= CTDB_CAP_LMASTER;
574 return 0;
577 case CTDB_CONTROL_SET_RECMASTERROLE: {
578 uint32_t recmasterrole;
580 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
581 recmasterrole = *(uint32_t *)indata.dptr;
582 if (recmasterrole == 0) {
583 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
584 } else {
585 ctdb->capabilities |= CTDB_CAP_RECMASTER;
587 return 0;
590 case CTDB_CONTROL_ENABLE_SCRIPT:
591 return ctdb_control_enable_script(ctdb, indata);
593 case CTDB_CONTROL_DISABLE_SCRIPT:
594 return ctdb_control_disable_script(ctdb, indata);
596 case CTDB_CONTROL_SET_BAN_STATE:
597 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_time));
598 return ctdb_control_set_ban_state(ctdb, indata);
600 case CTDB_CONTROL_GET_BAN_STATE:
601 CHECK_CONTROL_DATA_SIZE(0);
602 return ctdb_control_get_ban_state(ctdb, outdata);
604 case CTDB_CONTROL_SET_DB_PRIORITY:
605 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_db_priority));
606 return ctdb_control_set_db_priority(ctdb, indata);
608 case CTDB_CONTROL_GET_DB_PRIORITY: {
609 uint32_t db_id;
610 struct ctdb_db_context *ctdb_db;
612 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
613 db_id = *(uint32_t *)indata.dptr;
614 ctdb_db = find_ctdb_db(ctdb, db_id);
615 if (ctdb_db == NULL) return -1;
616 return ctdb_db->priority;
619 case CTDB_CONTROL_TRANSACTION_CANCEL:
620 CHECK_CONTROL_DATA_SIZE(0);
621 return ctdb_control_transaction_cancel(ctdb);
623 case CTDB_CONTROL_REGISTER_NOTIFY:
624 return ctdb_control_register_notify(ctdb, client_id, indata);
626 case CTDB_CONTROL_DEREGISTER_NOTIFY:
627 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_client_notify_deregister));
628 return ctdb_control_deregister_notify(ctdb, client_id, indata);
630 case CTDB_CONTROL_GET_LOG:
631 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_get_log_addr));
632 return ctdb_control_get_log(ctdb, indata);
634 case CTDB_CONTROL_CLEAR_LOG:
635 return ctdb_control_clear_log(ctdb);
637 case CTDB_CONTROL_GET_DB_SEQNUM:
638 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
639 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
641 case CTDB_CONTROL_DB_SET_HEALTHY:
642 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
643 return ctdb_control_db_set_healthy(ctdb, indata);
645 case CTDB_CONTROL_DB_GET_HEALTH:
646 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
647 return ctdb_control_db_get_health(ctdb, indata, outdata);
649 case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
650 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
651 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
653 case CTDB_CONTROL_GET_IFACES:
654 CHECK_CONTROL_DATA_SIZE(0);
655 return ctdb_control_get_ifaces(ctdb, c, outdata);
657 case CTDB_CONTROL_SET_IFACE_LINK_STATE:
658 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_iface_info));
659 return ctdb_control_set_iface_link(ctdb, c, indata);
661 case CTDB_CONTROL_GET_STAT_HISTORY:
662 CHECK_CONTROL_DATA_SIZE(0);
663 return ctdb_control_get_stat_history(ctdb, c, outdata);
665 case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
666 struct ctdb_control_schedule_for_deletion *d;
667 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
668 CHECK_CONTROL_MIN_DATA_SIZE(size);
669 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
670 size += d->keylen;
671 CHECK_CONTROL_DATA_SIZE(size);
672 return ctdb_control_schedule_for_deletion(ctdb, indata);
674 case CTDB_CONTROL_GET_DB_STATISTICS:
675 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
676 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
678 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
679 CHECK_CONTROL_DATA_SIZE(0);
680 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
682 case CTDB_CONTROL_RECEIVE_RECORDS:
683 return ctdb_control_receive_records(ctdb, indata, outdata);
685 default:
686 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
687 return -1;
692 send a reply for a ctdb control
694 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
695 TDB_DATA *outdata, int32_t status, const char *errormsg)
697 struct ctdb_reply_control *r;
698 size_t len;
700 /* some controls send no reply */
701 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
702 return;
705 len = offsetof(struct ctdb_reply_control, data) + (outdata?outdata->dsize:0);
706 if (errormsg) {
707 len += strlen(errormsg);
709 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control);
710 if (r == NULL) {
711 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
712 return;
715 r->hdr.destnode = c->hdr.srcnode;
716 r->hdr.reqid = c->hdr.reqid;
717 r->status = status;
718 r->datalen = outdata?outdata->dsize:0;
719 if (outdata && outdata->dsize) {
720 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
722 if (errormsg) {
723 r->errorlen = strlen(errormsg);
724 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
727 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
729 talloc_free(r);
733 called when a CTDB_REQ_CONTROL packet comes in
735 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
737 struct ctdb_req_control *c = (struct ctdb_req_control *)hdr;
738 TDB_DATA data, *outdata;
739 int32_t status;
740 bool async_reply = false;
741 const char *errormsg = NULL;
743 data.dptr = &c->data[0];
744 data.dsize = c->datalen;
746 outdata = talloc_zero(c, TDB_DATA);
748 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
749 &errormsg, &async_reply);
751 if (!async_reply) {
752 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
757 called when a CTDB_REPLY_CONTROL packet comes in
759 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
761 struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
762 TDB_DATA data;
763 struct ctdb_control_state *state;
764 const char *errormsg = NULL;
766 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_control_state);
767 if (state == NULL) {
768 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
769 ctdb->pnn, hdr->reqid));
770 return;
773 if (hdr->reqid != state->reqid) {
774 /* we found a record but it was the wrong one */
775 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
776 return;
779 data.dptr = &c->data[0];
780 data.dsize = c->datalen;
781 if (c->errorlen) {
782 errormsg = talloc_strndup(state,
783 (char *)&c->data[c->datalen], c->errorlen);
786 /* make state a child of the packet, so it goes away when the packet
787 is freed. */
788 talloc_steal(hdr, state);
790 state->callback(ctdb, c->status, data, errormsg, state->private_data);
793 static int ctdb_control_destructor(struct ctdb_control_state *state)
795 ctdb_reqid_remove(state->ctdb, state->reqid);
796 return 0;
800 handle a timeout of a control
802 static void ctdb_control_timeout(struct event_context *ev, struct timed_event *te,
803 struct timeval t, void *private_data)
805 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
806 TALLOC_CTX *tmp_ctx = talloc_new(ev);
808 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
810 talloc_steal(tmp_ctx, state);
812 state->callback(state->ctdb, -1, tdb_null,
813 "ctdb_control timed out",
814 state->private_data);
815 talloc_free(tmp_ctx);
820 send a control message to a node
822 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
823 uint64_t srvid, uint32_t opcode, uint32_t client_id,
824 uint32_t flags,
825 TDB_DATA data,
826 ctdb_control_callback_fn_t callback,
827 void *private_data)
829 struct ctdb_req_control *c;
830 struct ctdb_control_state *state;
831 size_t len;
833 if (ctdb->methods == NULL) {
834 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
835 return -1;
838 if (((destnode == CTDB_BROADCAST_VNNMAP) ||
839 (destnode == CTDB_BROADCAST_ALL) ||
840 (destnode == CTDB_BROADCAST_CONNECTED)) &&
841 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
842 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
843 return -1;
846 if (destnode != CTDB_BROADCAST_VNNMAP &&
847 destnode != CTDB_BROADCAST_ALL &&
848 destnode != CTDB_BROADCAST_CONNECTED &&
849 (!ctdb_validate_pnn(ctdb, destnode) ||
850 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
851 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
852 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
854 return 0;
857 /* the state is made a child of private_data if possible. This means any reply
858 will be discarded if the private_data goes away */
859 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
860 CTDB_NO_MEMORY(ctdb, state);
862 state->reqid = ctdb_reqid_new(ctdb, state);
863 state->callback = callback;
864 state->private_data = private_data;
865 state->ctdb = ctdb;
866 state->flags = flags;
868 talloc_set_destructor(state, ctdb_control_destructor);
870 len = offsetof(struct ctdb_req_control, data) + data.dsize;
871 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
872 struct ctdb_req_control);
873 CTDB_NO_MEMORY(ctdb, c);
874 talloc_set_name_const(c, "ctdb_req_control packet");
876 c->hdr.destnode = destnode;
877 c->hdr.reqid = state->reqid;
878 c->opcode = opcode;
879 c->client_id = client_id;
880 c->flags = flags;
881 c->srvid = srvid;
882 c->datalen = data.dsize;
883 if (data.dsize) {
884 memcpy(&c->data[0], data.dptr, data.dsize);
887 ctdb_queue_packet(ctdb, &c->hdr);
889 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
890 talloc_free(state);
891 return 0;
894 if (ctdb->tunable.control_timeout) {
895 event_add_timed(ctdb->ev, state,
896 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
897 ctdb_control_timeout, state);
900 talloc_free(c);
901 return 0;