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
) {
324 for (i
=0; i
<num_scripts
; i
++) {
325 ret
= ctdb_event_script_pull_elems(buf
+offset
,
347 static size_t ctdb_event_request_run_len(struct ctdb_event_request_run
*in
)
349 return ctdb_stringn_len(&in
->component
) +
350 ctdb_stringn_len(&in
->event
) +
351 ctdb_stringn_len(&in
->args
) +
352 ctdb_uint32_len(&in
->timeout
) +
353 ctdb_uint32_len(&in
->flags
);
356 static void ctdb_event_request_run_push(struct ctdb_event_request_run
*in
,
360 size_t offset
= 0, np
;
362 ctdb_stringn_push(&in
->component
, buf
+offset
, &np
);
365 ctdb_stringn_push(&in
->event
, buf
+offset
, &np
);
368 ctdb_stringn_push(&in
->args
, buf
+offset
, &np
);
371 ctdb_uint32_push(&in
->timeout
, buf
+offset
, &np
);
374 ctdb_uint32_push(&in
->flags
, buf
+offset
, &np
);
380 static int ctdb_event_request_run_pull(uint8_t *buf
,
383 struct ctdb_event_request_run
**out
,
386 struct ctdb_event_request_run
*value
;
387 size_t offset
= 0, np
;
390 value
= talloc(mem_ctx
, struct ctdb_event_request_run
);
395 ret
= ctdb_stringn_pull(buf
+offset
,
405 ret
= ctdb_stringn_pull(buf
+offset
,
415 ret
= ctdb_stringn_pull(buf
+offset
,
425 ret
= ctdb_uint32_pull(buf
+offset
,
434 ret
= ctdb_uint32_pull(buf
+offset
,
453 static size_t ctdb_event_request_status_len(
454 struct ctdb_event_request_status
*in
)
456 return ctdb_stringn_len(&in
->component
) +
457 ctdb_stringn_len(&in
->event
);
460 static void ctdb_event_request_status_push(
461 struct ctdb_event_request_status
*in
,
465 size_t offset
= 0, np
;
467 ctdb_stringn_push(&in
->component
, buf
+offset
, &np
);
470 ctdb_stringn_push(&in
->event
, buf
+offset
, &np
);
476 static int ctdb_event_request_status_pull(
480 struct ctdb_event_request_status
**out
,
483 struct ctdb_event_request_status
*value
;
484 size_t offset
= 0, np
;
487 value
= talloc(mem_ctx
, struct ctdb_event_request_status
);
492 ret
= ctdb_stringn_pull(buf
+offset
,
502 ret
= ctdb_stringn_pull(buf
+offset
,
522 static size_t ctdb_event_request_script_len(
523 struct ctdb_event_request_script
*in
)
525 return ctdb_stringn_len(&in
->component
) +
526 ctdb_stringn_len(&in
->script
) +
527 ctdb_event_script_action_len(in
->action
);
530 static void ctdb_event_request_script_push(
531 struct ctdb_event_request_script
*in
,
535 size_t offset
= 0, np
;
537 ctdb_stringn_push(&in
->component
, buf
+offset
, &np
);
540 ctdb_stringn_push(&in
->script
, buf
+offset
, &np
);
543 ctdb_event_script_action_push(in
->action
, buf
+offset
, &np
);
549 static int ctdb_event_request_script_pull(
553 struct ctdb_event_request_script
**out
,
556 struct ctdb_event_request_script
*value
;
557 size_t offset
= 0, np
;
560 value
= talloc(mem_ctx
, struct ctdb_event_request_script
);
565 ret
= ctdb_stringn_pull(buf
+offset
,
575 ret
= ctdb_stringn_pull(buf
+offset
,
585 ret
= ctdb_event_script_action_pull(buf
+offset
,
604 static size_t ctdb_event_reply_status_len(
605 struct ctdb_event_reply_status
*in
)
607 return ctdb_int32_len(&in
->summary
) +
608 ctdb_event_script_list_len(in
->script_list
);
611 static void ctdb_event_reply_status_push(
612 struct ctdb_event_reply_status
*in
,
616 size_t offset
= 0, np
;
618 ctdb_int32_push(&in
->summary
, buf
+offset
, &np
);
621 ctdb_event_script_list_push(in
->script_list
, buf
+offset
, &np
);
627 static int ctdb_event_reply_status_pull(
631 struct ctdb_event_reply_status
**out
,
634 struct ctdb_event_reply_status
*value
;
635 size_t offset
= 0, np
;
638 value
= talloc(mem_ctx
, struct ctdb_event_reply_status
);
643 ret
= ctdb_int32_pull(buf
+offset
, buflen
-offset
, &value
->summary
, &np
);
649 ret
= ctdb_event_script_list_pull(buf
+offset
,
669 static size_t ctdb_event_header_len(struct ctdb_event_header
*in
)
671 return ctdb_uint32_len(&in
->length
) +
672 ctdb_uint32_len(&in
->version
) +
673 ctdb_uint32_len(&in
->reqid
);
676 static void ctdb_event_header_push(struct ctdb_event_header
*in
,
680 size_t offset
= 0, np
;
682 ctdb_uint32_push(&in
->length
, buf
+offset
, &np
);
685 ctdb_uint32_push(&in
->version
, buf
+offset
, &np
);
688 ctdb_uint32_push(&in
->reqid
, buf
+offset
, &np
);
694 static int ctdb_event_header_pull(uint8_t *buf
,
696 struct ctdb_event_header
*value
,
699 size_t offset
= 0, np
;
702 ret
= ctdb_uint32_pull(buf
+offset
,
711 ret
= ctdb_uint32_pull(buf
+offset
,
720 ret
= ctdb_uint32_pull(buf
+offset
,
734 int ctdb_event_header_extract(uint8_t *buf
,
736 struct ctdb_event_header
*value
)
740 return ctdb_event_header_pull(buf
, buflen
, value
, &np
);
743 static size_t ctdb_event_request_data_len(struct ctdb_event_request
*in
)
747 len
= ctdb_event_command_len(in
->cmd
);
750 case CTDB_EVENT_CMD_RUN
:
751 len
+= ctdb_event_request_run_len(in
->data
.run
);
754 case CTDB_EVENT_CMD_STATUS
:
755 len
+= ctdb_event_request_status_len(in
->data
.status
);
758 case CTDB_EVENT_CMD_SCRIPT
:
759 len
+= ctdb_event_request_script_len(in
->data
.script
);
769 static void ctdb_event_request_data_push(struct ctdb_event_request
*in
,
773 size_t offset
= 0, np
;
775 ctdb_event_command_push(in
->cmd
, buf
+offset
, &np
);
779 case CTDB_EVENT_CMD_RUN
:
780 ctdb_event_request_run_push(in
->data
.run
, buf
+offset
, &np
);
783 case CTDB_EVENT_CMD_STATUS
:
784 ctdb_event_request_status_push(in
->data
.status
,
789 case CTDB_EVENT_CMD_SCRIPT
:
790 ctdb_event_request_script_push(in
->data
.script
,
803 static int ctdb_event_request_data_pull(uint8_t *buf
,
806 struct ctdb_event_request
**out
,
809 struct ctdb_event_request
*value
;
810 size_t offset
= 0, np
;
813 value
= talloc(mem_ctx
, struct ctdb_event_request
);
818 ret
= ctdb_event_command_pull(buf
+offset
,
827 switch (value
->cmd
) {
828 case CTDB_EVENT_CMD_RUN
:
829 ret
= ctdb_event_request_run_pull(buf
+offset
,
836 case CTDB_EVENT_CMD_STATUS
:
837 ret
= ctdb_event_request_status_pull(buf
+offset
,
844 case CTDB_EVENT_CMD_SCRIPT
:
845 ret
= ctdb_event_request_script_pull(buf
+offset
,
872 static size_t ctdb_event_reply_data_len(struct ctdb_event_reply
*in
)
876 len
= ctdb_event_command_len(in
->cmd
) +
877 ctdb_int32_len(&in
->result
);
879 if (in
->result
!= 0) {
884 case CTDB_EVENT_CMD_STATUS
:
885 len
+= ctdb_event_reply_status_len(in
->data
.status
);
896 static void ctdb_event_reply_data_push(struct ctdb_event_reply
*in
,
900 size_t offset
= 0, np
;
902 ctdb_event_command_push(in
->cmd
, buf
+offset
, &np
);
905 ctdb_int32_push(&in
->result
, buf
+offset
, &np
);
908 if (in
->result
!= 0) {
913 case CTDB_EVENT_CMD_STATUS
:
914 ctdb_event_reply_status_push(in
->data
.status
, buf
+offset
, &np
);
927 static int ctdb_event_reply_data_pull(uint8_t *buf
,
930 struct ctdb_event_reply
**out
,
933 struct ctdb_event_reply
*value
;
934 size_t offset
= 0, np
;
937 value
= talloc(mem_ctx
, struct ctdb_event_reply
);
942 ret
= ctdb_event_command_pull(buf
+offset
,
951 ret
= ctdb_int32_pull(buf
+offset
, buflen
-offset
, &value
->result
, &np
);
957 if (value
->result
!= 0) {
961 switch (value
->cmd
) {
962 case CTDB_EVENT_CMD_STATUS
:
963 ret
= ctdb_event_reply_status_pull(buf
+offset
,
991 size_t ctdb_event_request_len(struct ctdb_event_header
*h
,
992 struct ctdb_event_request
*in
)
994 return ctdb_event_header_len(h
) +
995 ctdb_event_request_data_len(in
);
998 int ctdb_event_request_push(struct ctdb_event_header
*h
,
999 struct ctdb_event_request
*in
,
1003 size_t len
, offset
= 0, np
;
1005 len
= ctdb_event_request_len(h
, in
);
1006 if (*buflen
< len
) {
1011 h
->length
= *buflen
;
1013 ctdb_event_header_push(h
, buf
+offset
, &np
);
1016 ctdb_event_request_data_push(in
, buf
+offset
, &np
);
1019 if (offset
> *buflen
) {
1026 int ctdb_event_request_pull(uint8_t *buf
,
1028 struct ctdb_event_header
*h
,
1029 TALLOC_CTX
*mem_ctx
,
1030 struct ctdb_event_request
**out
)
1032 size_t offset
= 0, np
;
1035 ret
= ctdb_event_header_pull(buf
+offset
, buflen
-offset
, h
, &np
);
1041 ret
= ctdb_event_request_data_pull(buf
+offset
,
1051 if (offset
> buflen
) {
1058 size_t ctdb_event_reply_len(struct ctdb_event_header
*h
,
1059 struct ctdb_event_reply
*in
)
1061 return ctdb_event_header_len(h
) +
1062 ctdb_event_reply_data_len(in
);
1065 int ctdb_event_reply_push(struct ctdb_event_header
*h
,
1066 struct ctdb_event_reply
*in
,
1070 size_t len
, offset
= 0, np
;
1072 len
= ctdb_event_reply_len(h
, in
);
1073 if (*buflen
< len
) {
1078 h
->length
= *buflen
;
1080 ctdb_event_header_push(h
, buf
+offset
, &np
);
1083 ctdb_event_reply_data_push(in
, buf
+offset
, &np
);
1086 if (offset
> *buflen
) {
1093 int ctdb_event_reply_pull(uint8_t *buf
,
1095 struct ctdb_event_header
*h
,
1096 TALLOC_CTX
*mem_ctx
,
1097 struct ctdb_event_reply
**out
)
1099 size_t offset
= 0, np
;
1102 ret
= ctdb_event_header_pull(buf
+offset
, buflen
-offset
, h
, &np
);
1108 ret
= ctdb_event_reply_data_pull(buf
+offset
,
1118 if (offset
> buflen
) {