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
:
24 case LTTNG_ACTION_TYPE_GROUP
:
26 case LTTNG_ACTION_TYPE_NOTIFY
:
28 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
29 return "ROTATE_SESSION";
30 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
31 return "SNAPSHOT_SESSION";
32 case LTTNG_ACTION_TYPE_START_SESSION
:
33 return "START_SESSION";
34 case LTTNG_ACTION_TYPE_STOP_SESSION
:
35 return "STOP_SESSION";
41 enum lttng_action_type
lttng_action_get_type(const struct lttng_action
*action
)
43 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
47 void lttng_action_init(
48 struct lttng_action
*action
,
49 enum lttng_action_type type
,
50 action_validate_cb validate
,
51 action_serialize_cb serialize
,
52 action_equal_cb equal
,
53 action_destroy_cb destroy
)
55 urcu_ref_init(&action
->ref
);
57 action
->validate
= validate
;
58 action
->serialize
= serialize
;
59 action
->equal
= equal
;
60 action
->destroy
= destroy
;
64 void action_destroy_ref(struct urcu_ref
*ref
)
66 struct lttng_action
*action
=
67 container_of(ref
, struct lttng_action
, ref
);
69 action
->destroy(action
);
73 void lttng_action_get(struct lttng_action
*action
)
75 urcu_ref_get(&action
->ref
);
79 void lttng_action_put(struct lttng_action
*action
)
85 assert(action
->destroy
);
86 urcu_ref_put(&action
->ref
, action_destroy_ref
);
89 void lttng_action_destroy(struct lttng_action
*action
)
91 lttng_action_put(action
);
95 bool lttng_action_validate(struct lttng_action
*action
)
104 if (!action
->validate
) {
105 /* Sub-class guarantees that it can never be invalid. */
110 valid
= action
->validate(action
);
116 int lttng_action_serialize(struct lttng_action
*action
,
117 struct lttng_payload
*payload
)
120 struct lttng_action_comm action_comm
= {
121 .action_type
= (int8_t) action
->type
,
124 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
125 sizeof(action_comm
));
130 ret
= action
->serialize(action
, payload
);
139 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
140 struct lttng_action
**action
)
142 ssize_t consumed_len
, specific_action_consumed_len
;
143 action_create_from_payload_cb create_from_payload_cb
;
144 const struct lttng_action_comm
*action_comm
;
145 const struct lttng_payload_view action_comm_view
=
146 lttng_payload_view_from_view(
147 view
, 0, sizeof(*action_comm
));
149 if (!view
|| !action
) {
154 if (!lttng_payload_view_is_valid(&action_comm_view
)) {
155 /* Payload not large enough to contain the header. */
160 action_comm
= (const struct lttng_action_comm
*) action_comm_view
.buffer
.data
;
162 DBG("Create action from payload: action-type=%s",
163 lttng_action_type_string(action_comm
->action_type
));
165 switch (action_comm
->action_type
) {
166 case LTTNG_ACTION_TYPE_NOTIFY
:
167 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
169 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
170 create_from_payload_cb
=
171 lttng_action_rotate_session_create_from_payload
;
173 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
174 create_from_payload_cb
=
175 lttng_action_snapshot_session_create_from_payload
;
177 case LTTNG_ACTION_TYPE_START_SESSION
:
178 create_from_payload_cb
=
179 lttng_action_start_session_create_from_payload
;
181 case LTTNG_ACTION_TYPE_STOP_SESSION
:
182 create_from_payload_cb
=
183 lttng_action_stop_session_create_from_payload
;
185 case LTTNG_ACTION_TYPE_GROUP
:
186 create_from_payload_cb
= lttng_action_group_create_from_payload
;
189 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
190 action_comm
->action_type
,
191 lttng_action_type_string(
192 action_comm
->action_type
));
198 /* Create buffer view for the action-type-specific data. */
199 struct lttng_payload_view specific_action_view
=
200 lttng_payload_view_from_view(view
,
201 sizeof(struct lttng_action_comm
),
204 specific_action_consumed_len
= create_from_payload_cb(
205 &specific_action_view
, action
);
207 if (specific_action_consumed_len
< 0) {
208 ERR("Failed to create specific action from buffer.");
215 consumed_len
= sizeof(struct lttng_action_comm
) +
216 specific_action_consumed_len
;
223 bool lttng_action_is_equal(const struct lttng_action
*a
,
224 const struct lttng_action
*b
)
226 bool is_equal
= false;
232 if (a
->type
!= b
->type
) {
242 is_equal
= a
->equal(a
, b
);