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 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
15 switch (action_type
) {
16 case LTTNG_ACTION_TYPE_UNKNOWN
:
18 case LTTNG_ACTION_TYPE_NOTIFY
:
25 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
27 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
31 enum lttng_action_type
lttng_action_get_type_const(
32 const struct lttng_action
*action
)
38 void lttng_action_init(
39 struct lttng_action
*action
,
40 enum lttng_action_type type
,
41 action_validate_cb validate
,
42 action_serialize_cb serialize
,
43 action_destroy_cb destroy
)
46 action
->validate
= validate
;
47 action
->serialize
= serialize
;
48 action
->destroy
= destroy
;
51 void lttng_action_destroy(struct lttng_action
*action
)
57 assert(action
->destroy
);
58 action
->destroy(action
);
62 bool lttng_action_validate(struct lttng_action
*action
)
71 if (!action
->validate
) {
72 /* Sub-class guarantees that it can never be invalid. */
77 valid
= action
->validate(action
);
83 int lttng_action_serialize(struct lttng_action
*action
,
84 struct lttng_dynamic_buffer
*buf
)
87 struct lttng_action_comm action_comm
= {
88 .action_type
= (int8_t) action
->type
,
91 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
97 ret
= action
->serialize(action
, buf
);
106 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
107 struct lttng_action
**action
)
109 ssize_t consumed_len
, specific_action_consumed_len
;
110 const struct lttng_action_comm
*action_comm
;
111 action_create_from_buffer_cb create_from_buffer_cb
;
112 struct lttng_buffer_view specific_action_view
;
114 if (!view
|| !action
) {
119 action_comm
= (const struct lttng_action_comm
*) view
->data
;
121 DBG("Create action from buffer: action-type=%s",
122 lttng_action_type_string(action_comm
->action_type
));
124 switch (action_comm
->action_type
) {
125 case LTTNG_ACTION_TYPE_NOTIFY
:
126 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
129 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
130 action_comm
->action_type
,
131 lttng_action_type_string(
132 action_comm
->action_type
));
137 /* Create buffer view for the action-type-specific data. */
138 specific_action_view
= lttng_buffer_view_from_view(view
,
139 sizeof(struct lttng_action_comm
),
140 view
->size
- sizeof(struct lttng_action_comm
));
142 specific_action_consumed_len
=
143 create_from_buffer_cb(&specific_action_view
, action
);
144 if (specific_action_consumed_len
< 0) {
145 ERR("Failed to create specific action from buffer.");
152 consumed_len
= sizeof(struct lttng_action_comm
) +
153 specific_action_consumed_len
;