2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/action/action-internal.h>
9 #include <lttng/action/notify-internal.h>
10 #include <common/error.h>
13 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
15 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
19 enum lttng_action_type
lttng_action_get_type_const(
20 const struct lttng_action
*action
)
26 void lttng_action_init(
27 struct lttng_action
*action
,
28 enum lttng_action_type type
,
29 action_validate_cb validate
,
30 action_serialize_cb serialize
,
31 action_destroy_cb destroy
)
34 action
->validate
= validate
;
35 action
->serialize
= serialize
;
36 action
->destroy
= destroy
;
39 void lttng_action_destroy(struct lttng_action
*action
)
45 assert(action
->destroy
);
46 action
->destroy(action
);
50 bool lttng_action_validate(struct lttng_action
*action
)
59 if (!action
->validate
) {
60 /* Sub-class guarantees that it can never be invalid. */
65 valid
= action
->validate(action
);
71 int lttng_action_serialize(struct lttng_action
*action
,
72 struct lttng_dynamic_buffer
*buf
)
75 struct lttng_action_comm action_comm
= {
76 .action_type
= (int8_t) action
->type
,
79 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
85 ret
= action
->serialize(action
, buf
);
94 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
95 struct lttng_action
**action
)
97 ssize_t consumed_len
, specific_action_consumed_len
;
98 const struct lttng_action_comm
*action_comm
;
99 action_create_from_buffer_cb create_from_buffer_cb
;
100 struct lttng_buffer_view specific_action_view
;
102 if (!view
|| !action
) {
107 action_comm
= (const struct lttng_action_comm
*) view
->data
;
109 DBG("Deserializing action from buffer");
110 switch (action_comm
->action_type
) {
111 case LTTNG_ACTION_TYPE_NOTIFY
:
112 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
119 /* Create buffer view for the action-type-specific data. */
120 specific_action_view
= lttng_buffer_view_from_view(view
,
121 sizeof(struct lttng_action_comm
),
122 view
->size
- sizeof(struct lttng_action_comm
));
124 specific_action_consumed_len
=
125 create_from_buffer_cb(&specific_action_view
, action
);
126 if (specific_action_consumed_len
< 0) {
127 ERR("Failed to create specific action from buffer.");
134 consumed_len
= sizeof(struct lttng_action_comm
) +
135 specific_action_consumed_len
;