2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/notify-internal.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_equal_cb equal
,
44 action_destroy_cb destroy
)
47 action
->validate
= validate
;
48 action
->serialize
= serialize
;
49 action
->equal
= equal
;
50 action
->destroy
= destroy
;
53 void lttng_action_destroy(struct lttng_action
*action
)
59 assert(action
->destroy
);
60 action
->destroy(action
);
64 bool lttng_action_validate(struct lttng_action
*action
)
73 if (!action
->validate
) {
74 /* Sub-class guarantees that it can never be invalid. */
79 valid
= action
->validate(action
);
85 int lttng_action_serialize(struct lttng_action
*action
,
86 struct lttng_dynamic_buffer
*buf
)
89 struct lttng_action_comm action_comm
= {
90 .action_type
= (int8_t) action
->type
,
93 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
99 ret
= action
->serialize(action
, buf
);
108 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
109 struct lttng_action
**action
)
111 ssize_t consumed_len
, specific_action_consumed_len
;
112 const struct lttng_action_comm
*action_comm
;
113 action_create_from_buffer_cb create_from_buffer_cb
;
114 struct lttng_buffer_view specific_action_view
;
116 if (!view
|| !action
) {
121 action_comm
= (const struct lttng_action_comm
*) view
->data
;
123 DBG("Create action from buffer: action-type=%s",
124 lttng_action_type_string(action_comm
->action_type
));
126 switch (action_comm
->action_type
) {
127 case LTTNG_ACTION_TYPE_NOTIFY
:
128 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
131 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
132 action_comm
->action_type
,
133 lttng_action_type_string(
134 action_comm
->action_type
));
139 /* Create buffer view for the action-type-specific data. */
140 specific_action_view
= lttng_buffer_view_from_view(view
,
141 sizeof(struct lttng_action_comm
),
142 view
->size
- sizeof(struct lttng_action_comm
));
144 specific_action_consumed_len
=
145 create_from_buffer_cb(&specific_action_view
, action
);
146 if (specific_action_consumed_len
< 0) {
147 ERR("Failed to create specific action from buffer.");
154 consumed_len
= sizeof(struct lttng_action_comm
) +
155 specific_action_consumed_len
;
162 bool lttng_action_is_equal(const struct lttng_action
*a
,
163 const struct lttng_action
*b
)
165 bool is_equal
= false;
171 if (a
->type
!= b
->type
) {
181 is_equal
= a
->equal(a
, b
);