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>
18 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
20 switch (action_type
) {
21 case LTTNG_ACTION_TYPE_UNKNOWN
:
23 case LTTNG_ACTION_TYPE_GROUP
:
25 case LTTNG_ACTION_TYPE_NOTIFY
:
27 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
28 return "ROTATE_SESSION";
29 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
30 return "SNAPSHOT_SESSION";
31 case LTTNG_ACTION_TYPE_START_SESSION
:
32 return "START_SESSION";
33 case LTTNG_ACTION_TYPE_STOP_SESSION
:
34 return "STOP_SESSION";
40 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
42 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
46 enum lttng_action_type
lttng_action_get_type_const(
47 const struct lttng_action
*action
)
53 void lttng_action_init(
54 struct lttng_action
*action
,
55 enum lttng_action_type type
,
56 action_validate_cb validate
,
57 action_serialize_cb serialize
,
58 action_equal_cb equal
,
59 action_destroy_cb destroy
)
61 urcu_ref_init(&action
->ref
);
63 action
->validate
= validate
;
64 action
->serialize
= serialize
;
65 action
->equal
= equal
;
66 action
->destroy
= destroy
;
70 void action_destroy_ref(struct urcu_ref
*ref
)
72 struct lttng_action
*action
=
73 container_of(ref
, struct lttng_action
, ref
);
75 action
->destroy(action
);
79 void lttng_action_get(struct lttng_action
*action
)
81 urcu_ref_get(&action
->ref
);
85 void lttng_action_put(struct lttng_action
*action
)
91 assert(action
->destroy
);
92 urcu_ref_put(&action
->ref
, action_destroy_ref
);
95 void lttng_action_destroy(struct lttng_action
*action
)
97 lttng_action_put(action
);
101 bool lttng_action_validate(struct lttng_action
*action
)
110 if (!action
->validate
) {
111 /* Sub-class guarantees that it can never be invalid. */
116 valid
= action
->validate(action
);
122 int lttng_action_serialize(struct lttng_action
*action
,
123 struct lttng_payload
*payload
)
126 struct lttng_action_comm action_comm
= {
127 .action_type
= (int8_t) action
->type
,
130 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
131 sizeof(action_comm
));
136 ret
= action
->serialize(action
, payload
);
145 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
146 struct lttng_action
**action
)
148 ssize_t consumed_len
, specific_action_consumed_len
;
149 const struct lttng_action_comm
*action_comm
;
150 action_create_from_payload_cb create_from_payload_cb
;
152 if (!view
|| !action
) {
157 action_comm
= (const struct lttng_action_comm
*) view
->buffer
.data
;
159 DBG("Create action from payload: action-type=%s",
160 lttng_action_type_string(action_comm
->action_type
));
162 switch (action_comm
->action_type
) {
163 case LTTNG_ACTION_TYPE_NOTIFY
:
164 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
166 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
167 create_from_payload_cb
=
168 lttng_action_rotate_session_create_from_payload
;
170 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
171 create_from_payload_cb
=
172 lttng_action_snapshot_session_create_from_payload
;
174 case LTTNG_ACTION_TYPE_START_SESSION
:
175 create_from_payload_cb
=
176 lttng_action_start_session_create_from_payload
;
178 case LTTNG_ACTION_TYPE_STOP_SESSION
:
179 create_from_payload_cb
=
180 lttng_action_stop_session_create_from_payload
;
182 case LTTNG_ACTION_TYPE_GROUP
:
183 create_from_payload_cb
= lttng_action_group_create_from_payload
;
186 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
187 action_comm
->action_type
,
188 lttng_action_type_string(
189 action_comm
->action_type
));
195 /* Create buffer view for the action-type-specific data. */
196 struct lttng_payload_view specific_action_view
=
197 lttng_payload_view_from_view(view
,
198 sizeof(struct lttng_action_comm
),
201 specific_action_consumed_len
= create_from_payload_cb(
202 &specific_action_view
, action
);
204 if (specific_action_consumed_len
< 0) {
205 ERR("Failed to create specific action from buffer.");
212 consumed_len
= sizeof(struct lttng_action_comm
) +
213 specific_action_consumed_len
;
220 bool lttng_action_is_equal(const struct lttng_action
*a
,
221 const struct lttng_action
*b
)
223 bool is_equal
= false;
229 if (a
->type
!= b
->type
) {
239 is_equal
= a
->equal(a
, b
);