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/group-internal.h>
12 #include <lttng/action/notify-internal.h>
13 #include <lttng/action/rotate-session-internal.h>
14 #include <lttng/action/snapshot-session-internal.h>
15 #include <lttng/action/start-session-internal.h>
16 #include <lttng/action/stop-session-internal.h>
19 const char *lttng_action_type_string(enum lttng_action_type action_type
)
21 switch (action_type
) {
22 case LTTNG_ACTION_TYPE_UNKNOWN
:
25 case LTTNG_ACTION_TYPE_GROUP
:
28 case LTTNG_ACTION_TYPE_NOTIFY
:
31 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
32 return "ROTATE_SESSION";
34 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
35 return "SNAPSHOT_SESSION";
37 case LTTNG_ACTION_TYPE_START_SESSION
:
38 return "START_SESSION";
40 case LTTNG_ACTION_TYPE_STOP_SESSION
:
41 return "STOP_SESSION";
48 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
50 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
54 enum lttng_action_type
lttng_action_get_type_const(
55 const struct lttng_action
*action
)
61 void lttng_action_init(
62 struct lttng_action
*action
,
63 enum lttng_action_type type
,
64 action_validate_cb validate
,
65 action_serialize_cb serialize
,
66 action_equal_cb equal
,
67 action_destroy_cb destroy
)
69 urcu_ref_init(&action
->ref
);
71 action
->validate
= validate
;
72 action
->serialize
= serialize
;
73 action
->equal
= equal
;
74 action
->destroy
= destroy
;
78 void action_destroy_ref(struct urcu_ref
*ref
)
80 struct lttng_action
*action
=
81 container_of(ref
, struct lttng_action
, ref
);
83 action
->destroy(action
);
87 void lttng_action_get(struct lttng_action
*action
)
89 urcu_ref_get(&action
->ref
);
93 void lttng_action_put(struct lttng_action
*action
)
99 assert(action
->destroy
);
100 urcu_ref_put(&action
->ref
, action_destroy_ref
);
103 void lttng_action_destroy(struct lttng_action
*action
)
105 lttng_action_put(action
);
109 bool lttng_action_validate(struct lttng_action
*action
)
118 if (!action
->validate
) {
119 /* Sub-class guarantees that it can never be invalid. */
124 valid
= action
->validate(action
);
130 int lttng_action_serialize(struct lttng_action
*action
,
131 struct lttng_dynamic_buffer
*buf
)
134 struct lttng_action_comm action_comm
= {
135 .action_type
= (int8_t) action
->type
,
138 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
139 sizeof(action_comm
));
144 ret
= action
->serialize(action
, buf
);
153 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
154 struct lttng_action
**action
)
156 ssize_t consumed_len
, specific_action_consumed_len
;
157 const struct lttng_action_comm
*action_comm
;
158 action_create_from_buffer_cb create_from_buffer_cb
;
159 struct lttng_buffer_view specific_action_view
;
161 if (!view
|| !action
) {
166 action_comm
= (const struct lttng_action_comm
*) view
->data
;
168 DBG("Create action from buffer: action-type=%s",
169 lttng_action_type_string(action_comm
->action_type
));
171 switch (action_comm
->action_type
) {
172 case LTTNG_ACTION_TYPE_NOTIFY
:
173 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
176 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
177 create_from_buffer_cb
=
178 lttng_action_rotate_session_create_from_buffer
;
181 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
182 create_from_buffer_cb
=
183 lttng_action_snapshot_session_create_from_buffer
;
186 case LTTNG_ACTION_TYPE_START_SESSION
:
187 create_from_buffer_cb
=
188 lttng_action_start_session_create_from_buffer
;
191 case LTTNG_ACTION_TYPE_STOP_SESSION
:
192 create_from_buffer_cb
=
193 lttng_action_stop_session_create_from_buffer
;
196 case LTTNG_ACTION_TYPE_GROUP
:
197 create_from_buffer_cb
= lttng_action_group_create_from_buffer
;
201 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
202 action_comm
->action_type
,
203 lttng_action_type_string(
204 action_comm
->action_type
));
209 /* Create buffer view for the action-type-specific data. */
210 specific_action_view
= lttng_buffer_view_from_view(view
,
211 sizeof(struct lttng_action_comm
),
212 view
->size
- sizeof(struct lttng_action_comm
));
214 specific_action_consumed_len
=
215 create_from_buffer_cb(&specific_action_view
, action
);
216 if (specific_action_consumed_len
< 0) {
217 ERR("Failed to create specific action from buffer.");
224 consumed_len
= sizeof(struct lttng_action_comm
) +
225 specific_action_consumed_len
;
232 bool lttng_action_is_equal(const struct lttng_action
*a
,
233 const struct lttng_action
*b
)
235 bool is_equal
= false;
241 if (a
->type
!= b
->type
) {
250 is_equal
= a
->equal
? a
->equal(a
, b
) : true;