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>
12 #include <lttng/action/rotate-session-internal.h>
13 #include <lttng/action/snapshot-session-internal.h>
14 #include <lttng/action/start-session-internal.h>
15 #include <lttng/action/stop-session-internal.h>
17 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
19 switch (action_type
) {
20 case LTTNG_ACTION_TYPE_UNKNOWN
:
22 case LTTNG_ACTION_TYPE_NOTIFY
:
24 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
25 return "ROTATE_SESSION";
26 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
27 return "SNAPSHOT_SESSION";
28 case LTTNG_ACTION_TYPE_START_SESSION
:
29 return "START_SESSION";
30 case LTTNG_ACTION_TYPE_STOP_SESSION
:
31 return "STOP_SESSION";
37 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
39 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
43 enum lttng_action_type
lttng_action_get_type_const(
44 const struct lttng_action
*action
)
50 void lttng_action_init(
51 struct lttng_action
*action
,
52 enum lttng_action_type type
,
53 action_validate_cb validate
,
54 action_serialize_cb serialize
,
55 action_equal_cb equal
,
56 action_destroy_cb destroy
)
58 urcu_ref_init(&action
->ref
);
60 action
->validate
= validate
;
61 action
->serialize
= serialize
;
62 action
->equal
= equal
;
63 action
->destroy
= destroy
;
67 void action_destroy_ref(struct urcu_ref
*ref
)
69 struct lttng_action
*action
=
70 container_of(ref
, struct lttng_action
, ref
);
72 action
->destroy(action
);
76 void lttng_action_get(struct lttng_action
*action
)
78 urcu_ref_get(&action
->ref
);
82 void lttng_action_put(struct lttng_action
*action
)
88 assert(action
->destroy
);
89 urcu_ref_put(&action
->ref
, action_destroy_ref
);
92 void lttng_action_destroy(struct lttng_action
*action
)
94 lttng_action_put(action
);
98 bool lttng_action_validate(struct lttng_action
*action
)
107 if (!action
->validate
) {
108 /* Sub-class guarantees that it can never be invalid. */
113 valid
= action
->validate(action
);
119 int lttng_action_serialize(struct lttng_action
*action
,
120 struct lttng_payload
*payload
)
123 struct lttng_action_comm action_comm
= {
124 .action_type
= (int8_t) action
->type
,
127 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
128 sizeof(action_comm
));
133 ret
= action
->serialize(action
, payload
);
142 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
143 struct lttng_action
**action
)
145 ssize_t consumed_len
, specific_action_consumed_len
;
146 const struct lttng_action_comm
*action_comm
;
147 action_create_from_payload_cb create_from_payload_cb
;
149 if (!view
|| !action
) {
154 action_comm
= (const struct lttng_action_comm
*) view
->buffer
.data
;
156 DBG("Create action from payload: action-type=%s",
157 lttng_action_type_string(action_comm
->action_type
));
159 switch (action_comm
->action_type
) {
160 case LTTNG_ACTION_TYPE_NOTIFY
:
161 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
163 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
164 create_from_payload_cb
=
165 lttng_action_rotate_session_create_from_payload
;
167 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
168 create_from_payload_cb
=
169 lttng_action_snapshot_session_create_from_payload
;
171 case LTTNG_ACTION_TYPE_START_SESSION
:
172 create_from_payload_cb
=
173 lttng_action_start_session_create_from_payload
;
175 case LTTNG_ACTION_TYPE_STOP_SESSION
:
176 create_from_payload_cb
=
177 lttng_action_stop_session_create_from_payload
;
180 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
181 action_comm
->action_type
,
182 lttng_action_type_string(
183 action_comm
->action_type
));
189 /* Create buffer view for the action-type-specific data. */
190 struct lttng_payload_view specific_action_view
=
191 lttng_payload_view_from_view(view
,
192 sizeof(struct lttng_action_comm
),
195 specific_action_consumed_len
= create_from_payload_cb(
196 &specific_action_view
, action
);
198 if (specific_action_consumed_len
< 0) {
199 ERR("Failed to create specific action from buffer.");
206 consumed_len
= sizeof(struct lttng_action_comm
) +
207 specific_action_consumed_len
;
214 bool lttng_action_is_equal(const struct lttng_action
*a
,
215 const struct lttng_action
*b
)
217 bool is_equal
= false;
223 if (a
->type
!= b
->type
) {
233 is_equal
= a
->equal(a
, b
);