2 CTDB event daemon protocol
4 Copyright (C) Amitay Isaacs 2018
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/>.
24 #include "protocol/protocol_basic.h"
26 #include "event_protocol.h"
27 #include "event_protocol_api.h"
29 static size_t ctdb_event_script_action_len(enum ctdb_event_script_action in)
33 return ctdb_uint32_len(&u32);
36 static void ctdb_event_script_action_push(enum ctdb_event_script_action in,
42 ctdb_uint32_push(&u32, buf, npush);
45 static int ctdb_event_script_action_pull(uint8_t *buf,
47 enum ctdb_event_script_action *out,
50 enum ctdb_event_script_action value;
55 ret = ctdb_uint32_pull(buf, buflen, &u32, &np);
62 value = CTDB_EVENT_SCRIPT_DISABLE;
66 value = CTDB_EVENT_SCRIPT_ENABLE;
79 static size_t ctdb_event_command_len(enum ctdb_event_command in)
83 return ctdb_uint32_len(&u32);
86 static void ctdb_event_command_push(enum ctdb_event_command in,
92 ctdb_uint32_push(&u32, buf, npush);
95 static int ctdb_event_command_pull(uint8_t *buf,
97 enum ctdb_event_command *out,
100 enum ctdb_event_command value;
105 ret = ctdb_uint32_pull(buf, buflen, &u32, &np);
112 value = CTDB_EVENT_CMD_RUN;
116 value = CTDB_EVENT_CMD_STATUS;
120 value = CTDB_EVENT_CMD_SCRIPT;
133 static size_t ctdb_event_script_len(struct ctdb_event_script *in)
135 return ctdb_stringn_len(&in->name) +
136 ctdb_timeval_len(&in->begin) +
137 ctdb_timeval_len(&in->end) +
138 ctdb_int32_len(&in->result) +
139 ctdb_stringn_len(&in->output);
142 static void ctdb_event_script_push(struct ctdb_event_script *in,
146 size_t offset = 0, np;
148 ctdb_stringn_push(&in->name, buf+offset, &np);
151 ctdb_timeval_push(&in->begin, buf+offset, &np);
154 ctdb_timeval_push(&in->end, buf+offset, &np);
157 ctdb_int32_push(&in->result, buf+offset, &np);
160 ctdb_stringn_push(&in->output, buf+offset, &np);
166 static int ctdb_event_script_pull_elems(uint8_t *buf,
169 struct ctdb_event_script *value,
172 size_t offset = 0, np;
175 ret = ctdb_stringn_pull(buf+offset,
185 ret = ctdb_timeval_pull(buf+offset,
194 ret = ctdb_timeval_pull(buf+offset,
203 ret = ctdb_int32_pull(buf+offset,
212 ret = ctdb_stringn_pull(buf+offset,
227 #ifdef EVENT_PROTOCOL_TEST
228 static int ctdb_event_script_pull(uint8_t *buf,
231 struct ctdb_event_script **out,
234 struct ctdb_event_script *value;
237 value = talloc(mem_ctx, struct ctdb_event_script);
242 ret = ctdb_event_script_pull_elems(buf, buflen, value, value, npull);
254 static size_t ctdb_event_script_list_len(struct ctdb_event_script_list *in)
259 len = ctdb_int32_len(&in->num_scripts);
261 for (i=0; i<in->num_scripts; i++) {
262 len += ctdb_event_script_len(&in->script[i]);
268 static void ctdb_event_script_list_push(struct ctdb_event_script_list *in,
272 size_t offset = 0, np;
275 ctdb_int32_push(&in->num_scripts, buf+offset, &np);
278 for (i=0; i<in->num_scripts; i++) {
279 ctdb_event_script_push(&in->script[i], buf+offset, &np);
286 static int ctdb_event_script_list_pull(uint8_t *buf,
289 struct ctdb_event_script_list **out,
292 struct ctdb_event_script_list *value = NULL;
293 size_t offset = 0, np;
297 ret = ctdb_int32_pull(buf+offset, buflen-offset, &num_scripts, &np);
303 if (num_scripts < 0) {
307 value = talloc_zero(mem_ctx, struct ctdb_event_script_list);
312 value->num_scripts = num_scripts;
313 if (num_scripts == 0) {
317 value->script = talloc_array(value, struct ctdb_event_script,
319 if (value->script == NULL) {
323 for (i=0; i<num_scripts; i++) {
324 ret = ctdb_event_script_pull_elems(buf+offset,
346 static size_t ctdb_event_request_run_len(struct ctdb_event_request_run *in)
348 return ctdb_stringn_len(&in->component) +
349 ctdb_stringn_len(&in->event) +
350 ctdb_stringn_len(&in->args) +
351 ctdb_uint32_len(&in->timeout) +
352 ctdb_uint32_len(&in->flags);
355 static void ctdb_event_request_run_push(struct ctdb_event_request_run *in,
359 size_t offset = 0, np;
361 ctdb_stringn_push(&in->component, buf+offset, &np);
364 ctdb_stringn_push(&in->event, buf+offset, &np);
367 ctdb_stringn_push(&in->args, buf+offset, &np);
370 ctdb_uint32_push(&in->timeout, buf+offset, &np);
373 ctdb_uint32_push(&in->flags, buf+offset, &np);
379 static int ctdb_event_request_run_pull(uint8_t *buf,
382 struct ctdb_event_request_run **out,
385 struct ctdb_event_request_run *value;
386 size_t offset = 0, np;
389 value = talloc(mem_ctx, struct ctdb_event_request_run);
394 ret = ctdb_stringn_pull(buf+offset,
404 ret = ctdb_stringn_pull(buf+offset,
414 ret = ctdb_stringn_pull(buf+offset,
424 ret = ctdb_uint32_pull(buf+offset,
433 ret = ctdb_uint32_pull(buf+offset,
452 static size_t ctdb_event_request_status_len(
453 struct ctdb_event_request_status *in)
455 return ctdb_stringn_len(&in->component) +
456 ctdb_stringn_len(&in->event);
459 static void ctdb_event_request_status_push(
460 struct ctdb_event_request_status *in,
464 size_t offset = 0, np;
466 ctdb_stringn_push(&in->component, buf+offset, &np);
469 ctdb_stringn_push(&in->event, buf+offset, &np);
475 static int ctdb_event_request_status_pull(
479 struct ctdb_event_request_status **out,
482 struct ctdb_event_request_status *value;
483 size_t offset = 0, np;
486 value = talloc(mem_ctx, struct ctdb_event_request_status);
491 ret = ctdb_stringn_pull(buf+offset,
501 ret = ctdb_stringn_pull(buf+offset,
521 static size_t ctdb_event_request_script_len(
522 struct ctdb_event_request_script *in)
524 return ctdb_stringn_len(&in->component) +
525 ctdb_stringn_len(&in->script) +
526 ctdb_event_script_action_len(in->action);
529 static void ctdb_event_request_script_push(
530 struct ctdb_event_request_script *in,
534 size_t offset = 0, np;
536 ctdb_stringn_push(&in->component, buf+offset, &np);
539 ctdb_stringn_push(&in->script, buf+offset, &np);
542 ctdb_event_script_action_push(in->action, buf+offset, &np);
548 static int ctdb_event_request_script_pull(
552 struct ctdb_event_request_script **out,
555 struct ctdb_event_request_script *value;
556 size_t offset = 0, np;
559 value = talloc(mem_ctx, struct ctdb_event_request_script);
564 ret = ctdb_stringn_pull(buf+offset,
574 ret = ctdb_stringn_pull(buf+offset,
584 ret = ctdb_event_script_action_pull(buf+offset,
603 static size_t ctdb_event_reply_status_len(
604 struct ctdb_event_reply_status *in)
606 return ctdb_int32_len(&in->summary) +
607 ctdb_event_script_list_len(in->script_list);
610 static void ctdb_event_reply_status_push(
611 struct ctdb_event_reply_status *in,
615 size_t offset = 0, np;
617 ctdb_int32_push(&in->summary, buf+offset, &np);
620 ctdb_event_script_list_push(in->script_list, buf+offset, &np);
626 static int ctdb_event_reply_status_pull(
630 struct ctdb_event_reply_status **out,
633 struct ctdb_event_reply_status *value;
634 size_t offset = 0, np;
637 value = talloc(mem_ctx, struct ctdb_event_reply_status);
642 ret = ctdb_int32_pull(buf+offset, buflen-offset, &value->summary, &np);
648 ret = ctdb_event_script_list_pull(buf+offset,
668 static size_t ctdb_event_header_len(struct ctdb_event_header *in)
670 return ctdb_uint32_len(&in->length) +
671 ctdb_uint32_len(&in->version) +
672 ctdb_uint32_len(&in->reqid);
675 static void ctdb_event_header_push(struct ctdb_event_header *in,
679 size_t offset = 0, np;
681 ctdb_uint32_push(&in->length, buf+offset, &np);
684 ctdb_uint32_push(&in->version, buf+offset, &np);
687 ctdb_uint32_push(&in->reqid, buf+offset, &np);
693 static int ctdb_event_header_pull(uint8_t *buf,
695 struct ctdb_event_header *value,
698 size_t offset = 0, np;
701 ret = ctdb_uint32_pull(buf+offset,
710 ret = ctdb_uint32_pull(buf+offset,
719 ret = ctdb_uint32_pull(buf+offset,
733 int ctdb_event_header_extract(uint8_t *buf,
735 struct ctdb_event_header *value)
739 return ctdb_event_header_pull(buf, buflen, value, &np);
742 static size_t ctdb_event_request_data_len(struct ctdb_event_request *in)
746 len = ctdb_event_command_len(in->cmd);
749 case CTDB_EVENT_CMD_RUN:
750 len += ctdb_event_request_run_len(in->data.run);
753 case CTDB_EVENT_CMD_STATUS:
754 len += ctdb_event_request_status_len(in->data.status);
757 case CTDB_EVENT_CMD_SCRIPT:
758 len += ctdb_event_request_script_len(in->data.script);
768 static void ctdb_event_request_data_push(struct ctdb_event_request *in,
772 size_t offset = 0, np;
774 ctdb_event_command_push(in->cmd, buf+offset, &np);
778 case CTDB_EVENT_CMD_RUN:
779 ctdb_event_request_run_push(in->data.run, buf+offset, &np);
782 case CTDB_EVENT_CMD_STATUS:
783 ctdb_event_request_status_push(in->data.status,
788 case CTDB_EVENT_CMD_SCRIPT:
789 ctdb_event_request_script_push(in->data.script,
802 static int ctdb_event_request_data_pull(uint8_t *buf,
805 struct ctdb_event_request **out,
808 struct ctdb_event_request *value;
809 size_t offset = 0, np;
812 value = talloc(mem_ctx, struct ctdb_event_request);
817 ret = ctdb_event_command_pull(buf+offset,
826 switch (value->cmd) {
827 case CTDB_EVENT_CMD_RUN:
828 ret = ctdb_event_request_run_pull(buf+offset,
835 case CTDB_EVENT_CMD_STATUS:
836 ret = ctdb_event_request_status_pull(buf+offset,
843 case CTDB_EVENT_CMD_SCRIPT:
844 ret = ctdb_event_request_script_pull(buf+offset,
871 static size_t ctdb_event_reply_data_len(struct ctdb_event_reply *in)
875 len = ctdb_event_command_len(in->cmd) +
876 ctdb_int32_len(&in->result);
878 if (in->result != 0) {
883 case CTDB_EVENT_CMD_STATUS:
884 len += ctdb_event_reply_status_len(in->data.status);
895 static void ctdb_event_reply_data_push(struct ctdb_event_reply *in,
899 size_t offset = 0, np;
901 ctdb_event_command_push(in->cmd, buf+offset, &np);
904 ctdb_int32_push(&in->result, buf+offset, &np);
907 if (in->result != 0) {
912 case CTDB_EVENT_CMD_STATUS:
913 ctdb_event_reply_status_push(in->data.status, buf+offset, &np);
926 static int ctdb_event_reply_data_pull(uint8_t *buf,
929 struct ctdb_event_reply **out,
932 struct ctdb_event_reply *value;
933 size_t offset = 0, np;
936 value = talloc(mem_ctx, struct ctdb_event_reply);
941 ret = ctdb_event_command_pull(buf+offset,
950 ret = ctdb_int32_pull(buf+offset, buflen-offset, &value->result, &np);
956 if (value->result != 0) {
960 switch (value->cmd) {
961 case CTDB_EVENT_CMD_STATUS:
962 ret = ctdb_event_reply_status_pull(buf+offset,
990 size_t ctdb_event_request_len(struct ctdb_event_header *h,
991 struct ctdb_event_request *in)
993 return ctdb_event_header_len(h) +
994 ctdb_event_request_data_len(in);
997 int ctdb_event_request_push(struct ctdb_event_header *h,
998 struct ctdb_event_request *in,
1002 size_t len, offset = 0, np;
1004 len = ctdb_event_request_len(h, in);
1005 if (*buflen < len) {
1010 h->length = *buflen;
1012 ctdb_event_header_push(h, buf+offset, &np);
1015 ctdb_event_request_data_push(in, buf+offset, &np);
1018 if (offset > *buflen) {
1025 int ctdb_event_request_pull(uint8_t *buf,
1027 struct ctdb_event_header *h,
1028 TALLOC_CTX *mem_ctx,
1029 struct ctdb_event_request **out)
1031 size_t offset = 0, np;
1034 ret = ctdb_event_header_pull(buf+offset, buflen-offset, h, &np);
1040 ret = ctdb_event_request_data_pull(buf+offset,
1050 if (offset > buflen) {
1057 size_t ctdb_event_reply_len(struct ctdb_event_header *h,
1058 struct ctdb_event_reply *in)
1060 return ctdb_event_header_len(h) +
1061 ctdb_event_reply_data_len(in);
1064 int ctdb_event_reply_push(struct ctdb_event_header *h,
1065 struct ctdb_event_reply *in,
1069 size_t len, offset = 0, np;
1071 len = ctdb_event_reply_len(h, in);
1072 if (*buflen < len) {
1077 h->length = *buflen;
1079 ctdb_event_header_push(h, buf+offset, &np);
1082 ctdb_event_reply_data_push(in, buf+offset, &np);
1085 if (offset > *buflen) {
1092 int ctdb_event_reply_pull(uint8_t *buf,
1094 struct ctdb_event_header *h,
1095 TALLOC_CTX *mem_ctx,
1096 struct ctdb_event_reply **out)
1098 size_t offset = 0, np;
1101 ret = ctdb_event_header_pull(buf+offset, buflen-offset, h, &np);
1107 ret = ctdb_event_reply_data_pull(buf+offset,
1117 if (offset > buflen) {