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/>.
20 #include "lib/events/events.h"
21 #include "lib/tdb/include/tdb.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
26 #include "lib/util/dlinklist.h"
30 struct ctdb_control_state {
31 struct ctdb_context *ctdb;
33 ctdb_control_callback_fn_t callback;
40 dump talloc memory hierarchy, returning it as a blob to the client
42 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
44 /* dump to a file, then send the file as a blob */
49 DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n", strerror(errno)));
52 talloc_report_full(NULL, f);
55 outdata->dptr = talloc_size(outdata, fsize);
56 CTDB_NO_MEMORY(ctdb, outdata->dptr);
57 outdata->dsize = fread(outdata->dptr, 1, fsize, f);
59 if (outdata->dsize != fsize) {
60 DEBUG(DEBUG_ERR,(__location__ " Unable to read tmpfile\n"));
68 process a control request
70 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb,
71 struct ctdb_req_control *c,
73 TDB_DATA *outdata, uint32_t srcnode,
74 const char **errormsg,
77 uint32_t opcode = c->opcode;
78 uint64_t srvid = c->srvid;
79 uint32_t client_id = c->client_id;
82 case CTDB_CONTROL_PROCESS_EXISTS: {
83 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
84 return kill(*(pid_t *)indata.dptr, 0);
87 case CTDB_CONTROL_SET_DEBUG: {
88 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
89 LogLevel = *(int32_t *)indata.dptr;
93 case CTDB_CONTROL_GET_DEBUG: {
94 CHECK_CONTROL_DATA_SIZE(0);
95 outdata->dptr = (uint8_t *)&LogLevel;
96 outdata->dsize = sizeof(LogLevel);
100 case CTDB_CONTROL_STATISTICS: {
101 CHECK_CONTROL_DATA_SIZE(0);
102 ctdb->statistics.memory_used = talloc_total_size(NULL);
103 ctdb->statistics.frozen = (ctdb->freeze_mode == CTDB_FREEZE_FROZEN);
104 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
105 outdata->dptr = (uint8_t *)&ctdb->statistics;
106 outdata->dsize = sizeof(ctdb->statistics);
110 case CTDB_CONTROL_GET_ALL_TUNABLES: {
111 CHECK_CONTROL_DATA_SIZE(0);
112 outdata->dptr = (uint8_t *)&ctdb->tunable;
113 outdata->dsize = sizeof(ctdb->tunable);
117 case CTDB_CONTROL_DUMP_MEMORY: {
118 CHECK_CONTROL_DATA_SIZE(0);
119 return ctdb_dump_memory(ctdb, outdata);
122 case CTDB_CONTROL_STATISTICS_RESET: {
123 CHECK_CONTROL_DATA_SIZE(0);
124 ZERO_STRUCT(ctdb->statistics);
128 case CTDB_CONTROL_GETVNNMAP:
129 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
131 case CTDB_CONTROL_GET_DBMAP:
132 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
134 case CTDB_CONTROL_GET_NODEMAPv4:
135 return ctdb_control_getnodemapv4(ctdb, opcode, indata, outdata);
137 case CTDB_CONTROL_GET_NODEMAP:
138 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
140 case CTDB_CONTROL_RELOAD_NODES_FILE:
141 CHECK_CONTROL_DATA_SIZE(0);
142 return ctdb_control_reload_nodes_file(ctdb, opcode);
144 case CTDB_CONTROL_SETVNNMAP:
145 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
147 case CTDB_CONTROL_PULL_DB:
148 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_pulldb));
149 return ctdb_control_pull_db(ctdb, indata, outdata);
151 case CTDB_CONTROL_SET_DMASTER:
152 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_dmaster));
153 return ctdb_control_set_dmaster(ctdb, indata);
155 case CTDB_CONTROL_PUSH_DB:
156 return ctdb_control_push_db(ctdb, indata);
158 case CTDB_CONTROL_GET_RECMODE: {
159 return ctdb->recovery_mode;
162 case CTDB_CONTROL_SET_RECMASTER: {
163 return ctdb_control_set_recmaster(ctdb, opcode, indata);
166 case CTDB_CONTROL_GET_RECMASTER:
167 return ctdb->recovery_master;
169 case CTDB_CONTROL_GET_PID:
172 case CTDB_CONTROL_GET_PNN:
175 case CTDB_CONTROL_PING:
176 CHECK_CONTROL_DATA_SIZE(0);
177 return ctdb->statistics.num_clients;
179 case CTDB_CONTROL_GET_DBNAME: {
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 outdata->dptr = discard_const(ctdb_db->db_name);
188 outdata->dsize = strlen(ctdb_db->db_name)+1;
192 case CTDB_CONTROL_GETDBPATH: {
194 struct ctdb_db_context *ctdb_db;
196 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
197 db_id = *(uint32_t *)indata.dptr;
198 ctdb_db = find_ctdb_db(ctdb, db_id);
199 if (ctdb_db == NULL) return -1;
200 outdata->dptr = discard_const(ctdb_db->db_path);
201 outdata->dsize = strlen(ctdb_db->db_path)+1;
205 case CTDB_CONTROL_DB_ATTACH:
206 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, false);
208 case CTDB_CONTROL_DB_ATTACH_PERSISTENT:
209 return ctdb_control_db_attach(ctdb, indata, outdata, srvid, true);
211 case CTDB_CONTROL_SET_CALL: {
212 struct ctdb_control_set_call *sc =
213 (struct ctdb_control_set_call *)indata.dptr;
214 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_call));
215 return ctdb_daemon_set_call(ctdb, sc->db_id, sc->fn, sc->id);
218 case CTDB_CONTROL_TRAVERSE_START:
219 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
220 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode, client_id);
222 case CTDB_CONTROL_TRAVERSE_ALL:
223 return ctdb_control_traverse_all(ctdb, indata, outdata);
225 case CTDB_CONTROL_TRAVERSE_DATA:
226 return ctdb_control_traverse_data(ctdb, indata, outdata);
228 case CTDB_CONTROL_TRAVERSE_KILL:
229 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
230 return ctdb_control_traverse_kill(ctdb, indata, outdata, srcnode);
232 case CTDB_CONTROL_REGISTER_SRVID:
233 return daemon_register_message_handler(ctdb, client_id, srvid);
235 case CTDB_CONTROL_DEREGISTER_SRVID:
236 return daemon_deregister_message_handler(ctdb, client_id, srvid);
238 case CTDB_CONTROL_ENABLE_SEQNUM:
239 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
240 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
242 case CTDB_CONTROL_UPDATE_SEQNUM:
243 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
244 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
246 case CTDB_CONTROL_FREEZE:
247 CHECK_CONTROL_DATA_SIZE(0);
248 return ctdb_control_freeze(ctdb, c, async_reply);
250 case CTDB_CONTROL_THAW:
251 CHECK_CONTROL_DATA_SIZE(0);
252 return ctdb_control_thaw(ctdb);
254 case CTDB_CONTROL_SET_RECMODE:
255 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
256 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
258 case CTDB_CONTROL_GET_MONMODE:
259 CHECK_CONTROL_DATA_SIZE(0);
260 return ctdb_monitoring_mode(ctdb);
262 case CTDB_CONTROL_ENABLE_MONITOR:
263 CHECK_CONTROL_DATA_SIZE(0);
264 ctdb_enable_monitoring(ctdb);
267 case CTDB_CONTROL_RUN_EVENTSCRIPTS:
268 return ctdb_run_eventscripts(ctdb, c, indata, async_reply);
270 case CTDB_CONTROL_DISABLE_MONITOR:
271 CHECK_CONTROL_DATA_SIZE(0);
272 ctdb_disable_monitoring(ctdb);
275 case CTDB_CONTROL_SHUTDOWN:
276 ctdb_stop_recoverd(ctdb);
277 ctdb_stop_keepalive(ctdb);
278 ctdb_stop_monitoring(ctdb);
279 ctdb_release_all_ips(ctdb);
280 if (ctdb->methods != NULL) {
281 ctdb->methods->shutdown(ctdb);
283 ctdb_event_script(ctdb, "shutdown");
284 DEBUG(DEBUG_NOTICE,("Received SHUTDOWN command. Stopping CTDB daemon.\n"));
287 case CTDB_CONTROL_TAKEOVER_IPv4:
288 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
289 return ctdb_control_takeover_ipv4(ctdb, c, indata, async_reply);
291 case CTDB_CONTROL_TAKEOVER_IP:
292 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
293 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
295 case CTDB_CONTROL_RELEASE_IPv4:
296 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ipv4));
297 return ctdb_control_release_ipv4(ctdb, c, indata, async_reply);
299 case CTDB_CONTROL_RELEASE_IP:
300 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
301 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
303 case CTDB_CONTROL_GET_PUBLIC_IPSv4:
304 CHECK_CONTROL_DATA_SIZE(0);
305 return ctdb_control_get_public_ipsv4(ctdb, c, outdata);
307 case CTDB_CONTROL_GET_PUBLIC_IPS:
308 CHECK_CONTROL_DATA_SIZE(0);
309 return ctdb_control_get_public_ips(ctdb, c, outdata);
311 case CTDB_CONTROL_TCP_CLIENT:
312 return ctdb_control_tcp_client(ctdb, client_id, indata);
314 case CTDB_CONTROL_STARTUP:
315 CHECK_CONTROL_DATA_SIZE(0);
316 return ctdb_control_startup(ctdb, srcnode);
318 case CTDB_CONTROL_TCP_ADD:
319 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_tcp_vnn));
320 return ctdb_control_tcp_add(ctdb, indata);
322 case CTDB_CONTROL_SET_TUNABLE:
323 return ctdb_control_set_tunable(ctdb, indata);
325 case CTDB_CONTROL_GET_TUNABLE:
326 return ctdb_control_get_tunable(ctdb, indata, outdata);
328 case CTDB_CONTROL_LIST_TUNABLES:
329 return ctdb_control_list_tunables(ctdb, outdata);
331 case CTDB_CONTROL_MODIFY_FLAGS:
332 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_flag_change));
333 return ctdb_control_modflags(ctdb, indata);
335 case CTDB_CONTROL_KILL_TCP:
336 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_killtcp));
337 return ctdb_control_kill_tcp(ctdb, indata);
339 case CTDB_CONTROL_GET_TCP_TICKLE_LIST:
340 CHECK_CONTROL_DATA_SIZE(sizeof(ctdb_sock_addr));
341 return ctdb_control_get_tcp_tickle_list(ctdb, indata, outdata);
343 case CTDB_CONTROL_SET_TCP_TICKLE_LIST:
344 /* data size is verified in the called function */
345 return ctdb_control_set_tcp_tickle_list(ctdb, indata);
347 case CTDB_CONTROL_REGISTER_SERVER_ID:
348 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
349 return ctdb_control_register_server_id(ctdb, client_id, indata);
351 case CTDB_CONTROL_UNREGISTER_SERVER_ID:
352 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
353 return ctdb_control_unregister_server_id(ctdb, indata);
355 case CTDB_CONTROL_CHECK_SERVER_ID:
356 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_server_id));
357 return ctdb_control_check_server_id(ctdb, indata);
359 case CTDB_CONTROL_GET_SERVER_ID_LIST:
360 CHECK_CONTROL_DATA_SIZE(0);
361 return ctdb_control_get_server_id_list(ctdb, outdata);
363 case CTDB_CONTROL_PERSISTENT_STORE:
364 return ctdb_control_persistent_store(ctdb, c, indata, async_reply);
366 case CTDB_CONTROL_UPDATE_RECORD:
367 return ctdb_control_update_record(ctdb, c, indata, async_reply);
369 case CTDB_CONTROL_SEND_GRATIOUS_ARP:
370 return ctdb_control_send_gratious_arp(ctdb, indata);
372 case CTDB_CONTROL_TRANSACTION_START:
373 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
374 return ctdb_control_transaction_start(ctdb, *(uint32_t *)indata.dptr);
376 case CTDB_CONTROL_TRANSACTION_COMMIT:
377 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
378 return ctdb_control_transaction_commit(ctdb, *(uint32_t *)indata.dptr);
380 case CTDB_CONTROL_WIPE_DATABASE:
381 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_wipe_database));
382 return ctdb_control_wipe_database(ctdb, indata);
384 case CTDB_CONTROL_UPTIME:
385 return ctdb_control_uptime(ctdb, outdata);
387 case CTDB_CONTROL_START_RECOVERY:
388 return ctdb_control_start_recovery(ctdb, c, async_reply);
390 case CTDB_CONTROL_END_RECOVERY:
391 return ctdb_control_end_recovery(ctdb, c, async_reply);
393 case CTDB_CONTROL_TRY_DELETE_RECORDS:
394 return ctdb_control_try_delete_records(ctdb, indata, outdata);
396 case CTDB_CONTROL_ADD_PUBLIC_IP:
397 return ctdb_control_add_public_address(ctdb, indata);
399 case CTDB_CONTROL_DEL_PUBLIC_IP:
400 return ctdb_control_del_public_address(ctdb, indata);
402 case CTDB_CONTROL_GET_CAPABILITIES:
403 return ctdb_control_get_capabilities(ctdb, outdata);
405 case CTDB_CONTROL_START_PERSISTENT_UPDATE:
406 return ctdb_control_start_persistent_update(ctdb, c, indata);
408 case CTDB_CONTROL_CANCEL_PERSISTENT_UPDATE:
409 return ctdb_control_cancel_persistent_update(ctdb, c, indata);
411 case CTDB_CONTROL_TRANS2_COMMIT:
412 case CTDB_CONTROL_TRANS2_COMMIT_RETRY:
413 return ctdb_control_trans2_commit(ctdb, c, indata, async_reply);
415 case CTDB_CONTROL_TRANS2_ERROR:
416 return ctdb_control_trans2_error(ctdb, c);
418 case CTDB_CONTROL_TRANS2_FINISHED:
419 return ctdb_control_trans2_finished(ctdb, c);
421 case CTDB_CONTROL_RECD_PING:
422 CHECK_CONTROL_DATA_SIZE(0);
423 return ctdb_control_recd_ping(ctdb);
425 case CTDB_CONTROL_EVENT_SCRIPT_INIT:
426 CHECK_CONTROL_DATA_SIZE(0);
427 return ctdb_control_event_script_init(ctdb);
429 case CTDB_CONTROL_EVENT_SCRIPT_START:
430 return ctdb_control_event_script_start(ctdb, indata);
432 case CTDB_CONTROL_EVENT_SCRIPT_STOP:
433 CHECK_CONTROL_DATA_SIZE(sizeof(int32_t));
434 return ctdb_control_event_script_stop(ctdb, indata);
436 case CTDB_CONTROL_EVENT_SCRIPT_FINISHED:
437 CHECK_CONTROL_DATA_SIZE(0);
438 return ctdb_control_event_script_finished(ctdb);
440 case CTDB_CONTROL_EVENT_SCRIPT_DISABLED:
441 return ctdb_control_event_script_disabled(ctdb, indata);
443 case CTDB_CONTROL_GET_EVENT_SCRIPT_STATUS:
444 CHECK_CONTROL_DATA_SIZE(0);
445 return ctdb_control_get_event_script_status(ctdb, outdata);
447 case CTDB_CONTROL_RECD_RECLOCK_LATENCY:
448 CHECK_CONTROL_DATA_SIZE(sizeof(double));
449 ctdb_reclock_latency(ctdb, "recd reclock", &ctdb->statistics.reclock.recd, *((double *)indata.dptr));
451 case CTDB_CONTROL_GET_RECLOCK_FILE:
452 CHECK_CONTROL_DATA_SIZE(0);
453 if (ctdb->recovery_lock_file != NULL) {
454 outdata->dptr = discard_const(ctdb->recovery_lock_file);
455 outdata->dsize = strlen(ctdb->recovery_lock_file) + 1;
458 case CTDB_CONTROL_SET_RECLOCK_FILE:
459 ctdb->tunable.verify_recovery_lock = 0;
460 if (ctdb->recovery_lock_file != NULL) {
461 talloc_free(ctdb->recovery_lock_file);
462 ctdb->recovery_lock_file = NULL;
464 if (indata.dsize > 0) {
465 ctdb->recovery_lock_file = talloc_strdup(ctdb, discard_const(indata.dptr));
468 case CTDB_CONTROL_STOP_NODE:
469 CHECK_CONTROL_DATA_SIZE(0);
470 return ctdb_control_stop_node(ctdb, c, async_reply);
472 case CTDB_CONTROL_CONTINUE_NODE:
473 CHECK_CONTROL_DATA_SIZE(0);
474 return ctdb_control_continue_node(ctdb);
476 case CTDB_CONTROL_SET_NATGWSTATE: {
479 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
480 natgwstate = *(uint32_t *)indata.dptr;
481 if (natgwstate == 0) {
482 ctdb->capabilities &= ~CTDB_CAP_NATGW;
484 ctdb->capabilities |= CTDB_CAP_NATGW;
489 case CTDB_CONTROL_SET_LMASTERROLE: {
490 uint32_t lmasterrole;
492 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
493 lmasterrole = *(uint32_t *)indata.dptr;
494 if (lmasterrole == 0) {
495 ctdb->capabilities &= ~CTDB_CAP_LMASTER;
497 ctdb->capabilities |= CTDB_CAP_LMASTER;
502 case CTDB_CONTROL_SET_RECMASTERROLE: {
503 uint32_t recmasterrole;
505 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
506 recmasterrole = *(uint32_t *)indata.dptr;
507 if (recmasterrole == 0) {
508 ctdb->capabilities &= ~CTDB_CAP_RECMASTER;
510 ctdb->capabilities |= CTDB_CAP_RECMASTER;
515 case CTDB_CONTROL_ENABLE_SCRIPT:
516 return ctdb_control_enable_script(ctdb, indata);
518 case CTDB_CONTROL_DISABLE_SCRIPT:
519 return ctdb_control_disable_script(ctdb, indata);
521 case CTDB_CONTROL_SET_BAN_STATE:
522 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_ban_time));
523 return ctdb_control_set_ban_state(ctdb, indata);
525 case CTDB_CONTROL_GET_BAN_STATE:
526 CHECK_CONTROL_DATA_SIZE(0);
527 return ctdb_control_get_ban_state(ctdb, outdata);
530 DEBUG(DEBUG_CRIT,(__location__ " Unknown CTDB control opcode %u\n", opcode));
536 send a reply for a ctdb control
538 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
539 TDB_DATA *outdata, int32_t status, const char *errormsg)
541 struct ctdb_reply_control *r;
544 /* some controls send no reply */
545 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
549 len = offsetof(struct ctdb_reply_control, data) + (outdata?outdata->dsize:0);
551 len += strlen(errormsg);
553 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control);
554 CTDB_NO_MEMORY_VOID(ctdb, r);
556 r->hdr.destnode = c->hdr.srcnode;
557 r->hdr.reqid = c->hdr.reqid;
559 r->datalen = outdata?outdata->dsize:0;
560 if (outdata && outdata->dsize) {
561 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
564 r->errorlen = strlen(errormsg);
565 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
568 ctdb_queue_packet_opcode(ctdb, &r->hdr, c->opcode);
574 called when a CTDB_REQ_CONTROL packet comes in
576 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
578 struct ctdb_req_control *c = (struct ctdb_req_control *)hdr;
579 TDB_DATA data, *outdata;
581 bool async_reply = False;
582 const char *errormsg = NULL;
584 data.dptr = &c->data[0];
585 data.dsize = c->datalen;
587 outdata = talloc_zero(c, TDB_DATA);
589 status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode,
590 &errormsg, &async_reply);
593 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
598 called when a CTDB_REPLY_CONTROL packet comes in
600 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
602 struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
604 struct ctdb_control_state *state;
605 const char *errormsg = NULL;
607 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_control_state);
609 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_control\n",
610 ctdb->pnn, hdr->reqid));
614 if (hdr->reqid != state->reqid) {
615 /* we found a record but it was the wrong one */
616 DEBUG(DEBUG_ERR, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
620 data.dptr = &c->data[0];
621 data.dsize = c->datalen;
623 errormsg = talloc_strndup(state,
624 (char *)&c->data[c->datalen], c->errorlen);
627 /* make state a child of the packet, so it goes away when the packet
629 talloc_steal(hdr, state);
631 state->callback(ctdb, c->status, data, errormsg, state->private_data);
634 static int ctdb_control_destructor(struct ctdb_control_state *state)
636 ctdb_reqid_remove(state->ctdb, state->reqid);
641 handle a timeout of a control
643 static void ctdb_control_timeout(struct event_context *ev, struct timed_event *te,
644 struct timeval t, void *private_data)
646 struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
647 TALLOC_CTX *tmp_ctx = talloc_new(ev);
649 state->ctdb->statistics.timeouts.control++;
651 talloc_steal(tmp_ctx, state);
653 state->callback(state->ctdb, -1, tdb_null,
654 "ctdb_control timed out",
655 state->private_data);
656 talloc_free(tmp_ctx);
661 send a control message to a node
663 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
664 uint64_t srvid, uint32_t opcode, uint32_t client_id,
667 ctdb_control_callback_fn_t callback,
670 struct ctdb_req_control *c;
671 struct ctdb_control_state *state;
674 if (ctdb->methods == NULL) {
675 DEBUG(DEBUG_ERR,(__location__ " Failed to send control. Transport is DOWN\n"));
679 if (((destnode == CTDB_BROADCAST_VNNMAP) ||
680 (destnode == CTDB_BROADCAST_ALL) ||
681 (destnode == CTDB_BROADCAST_CONNECTED)) &&
682 !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
683 DEBUG(DEBUG_CRIT,("Attempt to broadcast control without NOREPLY\n"));
687 if (destnode != CTDB_BROADCAST_VNNMAP &&
688 destnode != CTDB_BROADCAST_ALL &&
689 destnode != CTDB_BROADCAST_CONNECTED &&
690 (!ctdb_validate_pnn(ctdb, destnode) ||
691 (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
692 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
693 callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
698 /* the state is made a child of private_data if possible. This means any reply
699 will be discarded if the private_data goes away */
700 state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
701 CTDB_NO_MEMORY(ctdb, state);
703 state->reqid = ctdb_reqid_new(ctdb, state);
704 state->callback = callback;
705 state->private_data = private_data;
707 state->flags = flags;
709 talloc_set_destructor(state, ctdb_control_destructor);
711 len = offsetof(struct ctdb_req_control, data) + data.dsize;
712 c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len,
713 struct ctdb_req_control);
714 CTDB_NO_MEMORY(ctdb, c);
715 talloc_set_name_const(c, "ctdb_req_control packet");
717 c->hdr.destnode = destnode;
718 c->hdr.reqid = state->reqid;
720 c->client_id = client_id;
723 c->datalen = data.dsize;
725 memcpy(&c->data[0], data.dptr, data.dsize);
728 ctdb_queue_packet(ctdb, &c->hdr);
730 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
735 if (ctdb->tunable.control_timeout) {
736 event_add_timed(ctdb->ev, state,
737 timeval_current_ofs(ctdb->tunable.control_timeout, 0),
738 ctdb_control_timeout, state);