ctdb-daemon: Ignore SIGUSR1
[Samba.git] / ctdb / server / ctdb_control.c
blobfda4c29134a1a9e97050d036a480c78440c4c907
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 "lib/tdb_wrap/tdb_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 DEBUGLEVEL = *(int32_t *)indata.dptr;
112 return 0;
115 case CTDB_CONTROL_GET_DEBUG: {
116 CHECK_CONTROL_DATA_SIZE(0);
117 outdata->dptr = (uint8_t *)&(DEBUGLEVEL);
118 outdata->dsize = sizeof(DEBUGLEVEL);
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 control_not_implemented("GET_NODEMAPv4", "GET_NODEMAP");
169 case CTDB_CONTROL_GET_NODEMAP:
170 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
172 case CTDB_CONTROL_GET_NODES_FILE:
173 return ctdb_control_getnodesfile(ctdb, opcode, indata, outdata);
175 case CTDB_CONTROL_RELOAD_NODES_FILE:
176 CHECK_CONTROL_DATA_SIZE(0);
177 return ctdb_control_reload_nodes_file(ctdb, opcode);
179 case CTDB_CONTROL_SET_DB_STICKY: {
180 uint32_t db_id;
181 struct ctdb_db_context *ctdb_db;
183 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
184 db_id = *(uint32_t *)indata.dptr;
185 ctdb_db = find_ctdb_db(ctdb, db_id);
186 if (ctdb_db == NULL) return -1;
187 return ctdb_set_db_sticky(ctdb, ctdb_db);
190 case CTDB_CONTROL_SETVNNMAP:
191 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
193 case CTDB_CONTROL_PULL_DB:
194 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_pulldb));
195 return ctdb_control_pull_db(ctdb, indata, outdata);
197 case CTDB_CONTROL_SET_DMASTER:
198 return control_not_implemented("SET_DMASTER", NULL);
200 case CTDB_CONTROL_PUSH_DB:
201 return ctdb_control_push_db(ctdb, indata);
203 case CTDB_CONTROL_GET_RECMODE: {
204 return ctdb->recovery_mode;
207 case CTDB_CONTROL_SET_RECMASTER: {
208 return ctdb_control_set_recmaster(ctdb, opcode, indata);
211 case CTDB_CONTROL_GET_RECMASTER:
212 return ctdb->recovery_master;
214 case CTDB_CONTROL_GET_PID:
215 return getpid();
217 case CTDB_CONTROL_GET_PNN:
218 return ctdb->pnn;
220 case CTDB_CONTROL_PING:
221 CHECK_CONTROL_DATA_SIZE(0);
222 return ctdb->num_clients;
224 case CTDB_CONTROL_GET_RUNSTATE:
225 CHECK_CONTROL_DATA_SIZE(0);
226 outdata->dptr = (uint8_t *)&ctdb->runstate;
227 outdata->dsize = sizeof(uint32_t);
228 return 0;
231 case CTDB_CONTROL_SET_DB_READONLY: {
232 uint32_t db_id;
233 struct ctdb_db_context *ctdb_db;
235 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
236 db_id = *(uint32_t *)indata.dptr;
237 ctdb_db = find_ctdb_db(ctdb, db_id);
238 if (ctdb_db == NULL) return -1;
239 return ctdb_set_db_readonly(ctdb, ctdb_db);
241 case CTDB_CONTROL_GET_DBNAME: {
242 uint32_t db_id;
243 struct ctdb_db_context *ctdb_db;
245 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
246 db_id = *(uint32_t *)indata.dptr;
247 ctdb_db = find_ctdb_db(ctdb, db_id);
248 if (ctdb_db == NULL) return -1;
249 outdata->dptr = discard_const(ctdb_db->db_name);
250 outdata->dsize = strlen(ctdb_db->db_name)+1;
251 return 0;
254 case CTDB_CONTROL_GETDBPATH: {
255 uint32_t db_id;
256 struct ctdb_db_context *ctdb_db;
258 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
259 db_id = *(uint32_t *)indata.dptr;
260 ctdb_db = find_ctdb_db(ctdb, db_id);
261 if (ctdb_db == NULL) return -1;
262 outdata->dptr = discard_const(ctdb_db->db_path);
263 outdata->dsize = strlen(ctdb_db->db_path)+1;
264 return 0;
267 case CTDB_CONTROL_DB_ATTACH:
268 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, false, client_id, c, async_reply);
270 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
271 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, true, client_id, c, async_reply);
273 case CTDB_CONTROL_SET_CALL:
274 return control_not_implemented("SET_CALL", NULL);
276 case CTDB_CONTROL_TRAVERSE_START:
277 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
278 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
280 case CTDB_CONTROL_TRAVERSE_START_EXT:
281 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start_ext));
282 return ctdb_control_traverse_start_ext(ctdb, indata, outdata, srcnode, client_id);
284 case CTDB_CONTROL_TRAVERSE_ALL:
285 return ctdb_control_traverse_all(ctdb, indata, outdata);
287 case CTDB_CONTROL_TRAVERSE_ALL_EXT:
288 return ctdb_control_traverse_all_ext(ctdb, indata, outdata);
290 case CTDB_CONTROL_TRAVERSE_DATA:
291 return ctdb_control_traverse_data(ctdb, indata, outdata);
293 case CTDB_CONTROL_TRAVERSE_KILL:
294 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
295 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
297 case CTDB_CONTROL_REGISTER_SRVID:
298 return daemon_register_message_handler(ctdb, client_id, srvid);
300 case CTDB_CONTROL_DEREGISTER_SRVID:
301 return daemon_deregister_message_handler(ctdb, client_id, srvid);
303 case CTDB_CONTROL_CHECK_SRVIDS:
304 return daemon_check_srvids(ctdb, indata, outdata);
306 case CTDB_CONTROL_ENABLE_SEQNUM:
307 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
308 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
310 case CTDB_CONTROL_UPDATE_SEQNUM:
311 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
312 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
314 case CTDB_CONTROL_FREEZE:
315 CHECK_CONTROL_DATA_SIZE(0);
316 return ctdb_control_freeze(ctdb, c, async_reply);
318 case CTDB_CONTROL_THAW:
319 CHECK_CONTROL_DATA_SIZE(0);
320 return ctdb_control_thaw(ctdb, (uint32_t)c->srvid, true);
322 case CTDB_CONTROL_SET_RECMODE:
323 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
324 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
326 case CTDB_CONTROL_GET_MONMODE:
327 CHECK_CONTROL_DATA_SIZE(0);
328 return ctdb_monitoring_mode(ctdb);
330 case CTDB_CONTROL_ENABLE_MONITOR:
331 CHECK_CONTROL_DATA_SIZE(0);
332 ctdb_enable_monitoring(ctdb);
333 return 0;
335 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
336 return ctdb_run_eventscripts(ctdb, c, indata, async_reply);
338 case CTDB_CONTROL_DISABLE_MONITOR:
339 CHECK_CONTROL_DATA_SIZE(0);
340 ctdb_disable_monitoring(ctdb);
341 return 0;
343 case CTDB_CONTROL_SHUTDOWN:
344 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command.\n"));
345 ctdb_shutdown_sequence(ctdb, 0);
346 /* In case above returns due to duplicate shutdown */
347 return 0;
349 case CTDB_CONTROL_TAKEOVER_IPv4:
350 return control_not_implemented("TAKEOVER_IPv4", "TAKEOVER_IP");
352 case CTDB_CONTROL_TAKEOVER_IP:
353 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
354 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
356 case CTDB_CONTROL_RELEASE_IPv4:
357 return control_not_implemented("RELEASE_IPv4", "RELEASE_IP");
359 case CTDB_CONTROL_RELEASE_IP:
360 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
361 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
363 case CTDB_CONTROL_IPREALLOCATED:
364 CHECK_CONTROL_DATA_SIZE(0);
365 return ctdb_control_ipreallocated(ctdb, c, async_reply);
367 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
368 return control_not_implemented("GET_PUBLIC_IPSv4",
369 "GET_PUBLIC_IPS");
371 case CTDB_CONTROL_GET_PUBLIC_IPS:
372 CHECK_CONTROL_DATA_SIZE(0);
373 return ctdb_control_get_public_ips(ctdb, c, outdata);
375 case CTDB_CONTROL_TCP_CLIENT:
376 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_tcp_addr));
377 return ctdb_control_tcp_client(ctdb, client_id, indata);
379 case CTDB_CONTROL_STARTUP:
380 CHECK_CONTROL_DATA_SIZE(0);
381 return ctdb_control_startup(ctdb, srcnode);
383 case CTDB_CONTROL_TCP_ADD:
384 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
385 return ctdb_control_tcp_add(ctdb, indata, false);
387 case CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE:
388 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
389 return ctdb_control_tcp_add(ctdb, indata, true);
391 case CTDB_CONTROL_TCP_REMOVE:
392 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_tcp_connection));
393 return ctdb_control_tcp_remove(ctdb, indata);
395 case CTDB_CONTROL_SET_TUNABLE:
396 return ctdb_control_set_tunable(ctdb, indata);
398 case CTDB_CONTROL_GET_TUNABLE:
399 return ctdb_control_get_tunable(ctdb, indata, outdata);
401 case CTDB_CONTROL_LIST_TUNABLES:
402 return ctdb_control_list_tunables(ctdb, outdata);
404 case CTDB_CONTROL_MODIFY_FLAGS:
405 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
406 return ctdb_control_modflags(ctdb, indata);
408 case CTDB_CONTROL_KILL_TCP:
409 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_killtcp));
410 return ctdb_control_kill_tcp(ctdb, indata);
412 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
413 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
414 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
416 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
417 /* data size is verified in the called function */
418 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
420 case CTDB_CONTROL_REGISTER_SERVER_ID:
421 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
422 return ctdb_control_register_server_id(ctdb, client_id, indata);
424 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
425 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
426 return ctdb_control_unregister_server_id(ctdb, indata);
428 case CTDB_CONTROL_CHECK_SERVER_ID:
429 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
430 return ctdb_control_check_server_id(ctdb, indata);
432 case CTDB_CONTROL_GET_SERVER_ID_LIST:
433 CHECK_CONTROL_DATA_SIZE(0);
434 return ctdb_control_get_server_id_list(ctdb, outdata);
436 case CTDB_CONTROL_PERSISTENT_STORE:
437 return control_not_implemented("PERSISTENT_STORE", NULL);
439 case CTDB_CONTROL_UPDATE_RECORD:
440 return ctdb_control_update_record(ctdb, c, indata, async_reply);
442 case CTDB_CONTROL_SEND_GRATIOUS_ARP:
443 return ctdb_control_send_gratious_arp(ctdb, indata);
445 case CTDB_CONTROL_TRANSACTION_START:
446 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
447 return ctdb_control_transaction_start(ctdb, *(uint32_t *)indata.dptr);
449 case CTDB_CONTROL_TRANSACTION_COMMIT:
450 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
451 return ctdb_control_transaction_commit(ctdb, *(uint32_t *)indata.dptr);
453 case CTDB_CONTROL_WIPE_DATABASE:
454 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_wipe_database));
455 return ctdb_control_wipe_database(ctdb, indata);
457 case CTDB_CONTROL_UPTIME:
458 return ctdb_control_uptime(ctdb, outdata);
460 case CTDB_CONTROL_START_RECOVERY:
461 return ctdb_control_start_recovery(ctdb, c, async_reply);
463 case CTDB_CONTROL_END_RECOVERY:
464 return ctdb_control_end_recovery(ctdb, c, async_reply);
466 case CTDB_CONTROL_TRY_DELETE_RECORDS:
467 return ctdb_control_try_delete_records(ctdb, indata, outdata);
469 case CTDB_CONTROL_ADD_PUBLIC_IP:
470 return ctdb_control_add_public_address(ctdb, indata);
472 case CTDB_CONTROL_DEL_PUBLIC_IP:
473 return ctdb_control_del_public_address(ctdb, c, indata,
474 async_reply);
476 case CTDB_CONTROL_GET_CAPABILITIES:
477 return ctdb_control_get_capabilities(ctdb, outdata);
479 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
480 return ctdb_control_start_persistent_update(ctdb, c, indata);
482 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
483 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
485 case CTDB_CONTROL_TRANS2_COMMIT:
486 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
487 return control_not_implemented("TRANS2_COMMIT", "TRANS3_COMMIT");
489 case CTDB_CONTROL_TRANS2_ERROR:
490 return control_not_implemented("TRANS2_ERROR", NULL);
492 case CTDB_CONTROL_TRANS2_FINISHED:
493 return control_not_implemented("TRANS2_FINISHED", NULL);
495 case CTDB_CONTROL_TRANS2_ACTIVE:
496 return control_not_implemented("TRANS2_ACTIVE", NULL);
498 case CTDB_CONTROL_TRANS3_COMMIT:
499 return ctdb_control_trans3_commit(ctdb, c, indata, async_reply);
501 case CTDB_CONTROL_RECD_PING:
502 CHECK_CONTROL_DATA_SIZE(0);
503 return ctdb_control_recd_ping(ctdb);
505 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
506 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
507 return ctdb_control_get_event_script_status(ctdb, *(uint32_t *)indata.dptr, outdata);
509 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
510 CHECK_CONTROL_DATA_SIZE(sizeof(double));
511 CTDB_UPDATE_RECLOCK_LATENCY(ctdb, "recd reclock", reclock.recd, *((double *)indata.dptr));
512 return 0;
513 case CTDB_CONTROL_GET_RECLOCK_FILE:
514 CHECK_CONTROL_DATA_SIZE(0);
515 if (ctdb->recovery_lock_file != NULL) {
516 outdata->dptr = discard_const(ctdb->recovery_lock_file);
517 outdata->dsize = strlen(ctdb->recovery_lock_file) + 1;
519 return 0;
520 case CTDB_CONTROL_SET_RECLOCK_FILE: {
521 char *t;
523 if (indata.dsize == 0) {
524 TALLOC_FREE(ctdb->recovery_lock_file);
525 return 0;
528 /* Return silent success if unchanged. Recovery
529 * master updates all nodes on each recovery - we
530 * don't need the extra memory allocation or log
531 * message each time. */
532 if (ctdb->recovery_lock_file != NULL &&
533 strcmp(discard_const(indata.dptr),
534 ctdb->recovery_lock_file) == 0) {
535 return 0;
538 t = talloc_strdup(ctdb, discard_const(indata.dptr));
539 if (t == NULL) {
540 DEBUG(DEBUG_ERR, ("Out of memory in SET_RECLOCK_FILE\n"));
541 return -1;
544 talloc_free(ctdb->recovery_lock_file);
545 ctdb->recovery_lock_file = t;
546 DEBUG(DEBUG_NOTICE, ("Updated recovery lock file to %s\n", t));
548 return 0;
551 case CTDB_CONTROL_STOP_NODE:
552 CHECK_CONTROL_DATA_SIZE(0);
553 return ctdb_control_stop_node(ctdb);
555 case CTDB_CONTROL_CONTINUE_NODE:
556 CHECK_CONTROL_DATA_SIZE(0);
557 return ctdb_control_continue_node(ctdb);
559 case CTDB_CONTROL_SET_NATGWSTATE: {
560 uint32_t natgwstate;
562 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
563 natgwstate = *(uint32_t *)indata.dptr;
564 if (natgwstate == 0) {
565 ctdb->capabilities &= ~CTDB_CAP_NATGW;
566 } else {
567 ctdb->capabilities |= CTDB_CAP_NATGW;
569 return 0;
572 case CTDB_CONTROL_SET_LMASTERROLE: {
573 uint32_t lmasterrole;
575 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
576 lmasterrole = *(uint32_t *)indata.dptr;
577 if (lmasterrole == 0) {
578 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
579 } else {
580 ctdb->capabilities |= CTDB_CAP_LMASTER;
582 return 0;
585 case CTDB_CONTROL_SET_RECMASTERROLE: {
586 uint32_t recmasterrole;
588 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
589 recmasterrole = *(uint32_t *)indata.dptr;
590 if (recmasterrole == 0) {
591 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
592 } else {
593 ctdb->capabilities |= CTDB_CAP_RECMASTER;
595 return 0;
598 case CTDB_CONTROL_ENABLE_SCRIPT:
599 return ctdb_control_enable_script(ctdb, indata);
601 case CTDB_CONTROL_DISABLE_SCRIPT:
602 return ctdb_control_disable_script(ctdb, indata);
604 case CTDB_CONTROL_SET_BAN_STATE:
605 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_time));
606 return ctdb_control_set_ban_state(ctdb, indata);
608 case CTDB_CONTROL_GET_BAN_STATE:
609 CHECK_CONTROL_DATA_SIZE(0);
610 return ctdb_control_get_ban_state(ctdb, outdata);
612 case CTDB_CONTROL_SET_DB_PRIORITY:
613 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_db_priority));
614 return ctdb_control_set_db_priority(ctdb, indata, client_id);
616 case CTDB_CONTROL_GET_DB_PRIORITY: {
617 uint32_t db_id;
618 struct ctdb_db_context *ctdb_db;
620 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
621 db_id = *(uint32_t *)indata.dptr;
622 ctdb_db = find_ctdb_db(ctdb, db_id);
623 if (ctdb_db == NULL) return -1;
624 return ctdb_db->priority;
627 case CTDB_CONTROL_TRANSACTION_CANCEL:
628 CHECK_CONTROL_DATA_SIZE(0);
629 return ctdb_control_transaction_cancel(ctdb);
631 case CTDB_CONTROL_REGISTER_NOTIFY:
632 return ctdb_control_register_notify(ctdb, client_id, indata);
634 case CTDB_CONTROL_DEREGISTER_NOTIFY:
635 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_client_notify_deregister));
636 return ctdb_control_deregister_notify(ctdb, client_id, indata);
638 case CTDB_CONTROL_GET_LOG:
639 return control_not_implemented("GET_LOG", NULL);
641 case CTDB_CONTROL_CLEAR_LOG:
642 return control_not_implemented("CLEAR_LOG", NULL);
644 case CTDB_CONTROL_GET_DB_SEQNUM:
645 CHECK_CONTROL_DATA_SIZE(sizeof(uint64_t));
646 return ctdb_control_get_db_seqnum(ctdb, indata, outdata);
648 case CTDB_CONTROL_DB_SET_HEALTHY:
649 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
650 return ctdb_control_db_set_healthy(ctdb, indata);
652 case CTDB_CONTROL_DB_GET_HEALTH:
653 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
654 return ctdb_control_db_get_health(ctdb, indata, outdata);
656 case CTDB_CONTROL_GET_PUBLIC_IP_INFO:
657 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
658 return ctdb_control_get_public_ip_info(ctdb, c, indata, outdata);
660 case CTDB_CONTROL_GET_IFACES:
661 CHECK_CONTROL_DATA_SIZE(0);
662 return ctdb_control_get_ifaces(ctdb, c, outdata);
664 case CTDB_CONTROL_SET_IFACE_LINK_STATE:
665 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_iface_info));
666 return ctdb_control_set_iface_link(ctdb, c, indata);
668 case CTDB_CONTROL_GET_STAT_HISTORY:
669 CHECK_CONTROL_DATA_SIZE(0);
670 return ctdb_control_get_stat_history(ctdb, c, outdata);
672 case CTDB_CONTROL_SCHEDULE_FOR_DELETION: {
673 struct ctdb_control_schedule_for_deletion *d;
674 size_t size = offsetof(struct ctdb_control_schedule_for_deletion, key);
675 CHECK_CONTROL_MIN_DATA_SIZE(size);
676 d = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
677 size += d->keylen;
678 CHECK_CONTROL_DATA_SIZE(size);
679 return ctdb_control_schedule_for_deletion(ctdb, indata);
681 case CTDB_CONTROL_GET_DB_STATISTICS:
682 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
683 return ctdb_control_get_db_statistics(ctdb, *(uint32_t *)indata.dptr, outdata);
685 case CTDB_CONTROL_RELOAD_PUBLIC_IPS:
686 CHECK_CONTROL_DATA_SIZE(0);
687 return ctdb_control_reload_public_ips(ctdb, c, async_reply);
689 case CTDB_CONTROL_RECEIVE_RECORDS:
690 return ctdb_control_receive_records(ctdb, indata, outdata);
692 case CTDB_CONTROL_DB_DETACH:
693 return ctdb_control_db_detach(ctdb, indata, client_id);
695 default:
696 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
697 return -1;
702 send a reply for a ctdb control
704 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
705 TDB_DATA *outdata, int32_t status, const char *errormsg)
707 struct ctdb_reply_control *r;
708 size_t len;
710 /* some controls send no reply */
711 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
712 return;
715 len = offsetof(struct ctdb_reply_control, data) + (outdata?outdata->dsize:0);
716 if (errormsg) {
717 len += strlen(errormsg);
719 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control);
720 if (r == NULL) {
721 DEBUG(DEBUG_ERR,(__location__ "Unable to allocate transport - OOM or transport is down\n"));
722 return;
725 r->hdr.destnode = c->hdr.srcnode;
726 r->hdr.reqid = c->hdr.reqid;
727 r->status = status;
728 r->datalen = outdata?outdata->dsize:0;
729 if (outdata && outdata->dsize) {
730 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
732 if (errormsg) {
733 r->errorlen = strlen(errormsg);
734 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
737 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
739 talloc_free(r);
743 called when a CTDB_REQ_CONTROL packet comes in
745 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
747 struct ctdb_req_control *c = (struct ctdb_req_control *)hdr;
748 TDB_DATA data, *outdata;
749 int32_t status;
750 bool async_reply = false;
751 const char *errormsg = NULL;
753 data.dptr = &c->data[0];
754 data.dsize = c->datalen;
756 outdata = talloc_zero(c, TDB_DATA);
758 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
759 &errormsg, &async_reply);
761 if (!async_reply) {
762 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
767 called when a CTDB_REPLY_CONTROL packet comes in
769 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
771 struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
772 TDB_DATA data;
773 struct ctdb_control_state *state;
774 const char *errormsg = NULL;
776 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_control_state);
777 if (state == NULL) {
778 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
779 ctdb->pnn, hdr->reqid));
780 return;
783 if (hdr->reqid != state->reqid) {
784 /* we found a record but it was the wrong one */
785 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
786 return;
789 data.dptr = &c->data[0];
790 data.dsize = c->datalen;
791 if (c->errorlen) {
792 errormsg = talloc_strndup(state,
793 (char *)&c->data[c->datalen], c->errorlen);
796 /* make state a child of the packet, so it goes away when the packet
797 is freed. */
798 talloc_steal(hdr, state);
800 state->callback(ctdb, c->status, data, errormsg, state->private_data);
803 static int ctdb_control_destructor(struct ctdb_control_state *state)
805 ctdb_reqid_remove(state->ctdb, state->reqid);
806 return 0;
810 handle a timeout of a control
812 static void ctdb_control_timeout(struct event_context *ev, struct timed_event *te,
813 struct timeval t, void *private_data)
815 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
816 TALLOC_CTX *tmp_ctx = talloc_new(ev);
818 CTDB_INCREMENT_STAT(state->ctdb, timeouts.control);
820 talloc_steal(tmp_ctx, state);
822 state->callback(state->ctdb, -1, tdb_null,
823 "ctdb_control timed out",
824 state->private_data);
825 talloc_free(tmp_ctx);
830 send a control message to a node
832 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
833 uint64_t srvid, uint32_t opcode, uint32_t client_id,
834 uint32_t flags,
835 TDB_DATA data,
836 ctdb_control_callback_fn_t callback,
837 void *private_data)
839 struct ctdb_req_control *c;
840 struct ctdb_control_state *state;
841 size_t len;
843 if (ctdb->methods == NULL) {
844 DEBUG(DEBUG_INFO,(__location__ " Failed to send control. Transport is DOWN\n"));
845 return -1;
848 if (((destnode == CTDB_BROADCAST_VNNMAP) ||
849 (destnode == CTDB_BROADCAST_ALL) ||
850 (destnode == CTDB_BROADCAST_CONNECTED)) &&
851 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
852 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
853 return -1;
856 if (destnode != CTDB_BROADCAST_VNNMAP &&
857 destnode != CTDB_BROADCAST_ALL &&
858 destnode != CTDB_BROADCAST_CONNECTED &&
859 (!ctdb_validate_pnn(ctdb, destnode) ||
860 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
861 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
862 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
864 return 0;
867 /* the state is made a child of private_data if possible. This means any reply
868 will be discarded if the private_data goes away */
869 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
870 CTDB_NO_MEMORY(ctdb, state);
872 state->reqid = ctdb_reqid_new(ctdb, state);
873 state->callback = callback;
874 state->private_data = private_data;
875 state->ctdb = ctdb;
876 state->flags = flags;
878 talloc_set_destructor(state, ctdb_control_destructor);
880 len = offsetof(struct ctdb_req_control, data) + data.dsize;
881 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
882 struct ctdb_req_control);
883 CTDB_NO_MEMORY(ctdb, c);
884 talloc_set_name_const(c, "ctdb_req_control packet");
886 c->hdr.destnode = destnode;
887 c->hdr.reqid = state->reqid;
888 c->opcode = opcode;
889 c->client_id = client_id;
890 c->flags = flags;
891 c->srvid = srvid;
892 c->datalen = data.dsize;
893 if (data.dsize) {
894 memcpy(&c->data[0], data.dptr, data.dsize);
897 ctdb_queue_packet(ctdb, &c->hdr);
899 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
900 talloc_free(state);
901 return 0;
904 if (ctdb->tunable.control_timeout) {
905 event_add_timed(ctdb->ev, state,
906 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
907 ctdb_control_timeout, state);
910 talloc_free(c);
911 return 0;