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(struct lttng_action
*action
)
43 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
47 enum lttng_action_type
lttng_action_get_type_const(
48 const struct lttng_action
*action
)
54 void lttng_action_init(
55 struct lttng_action
*action
,
56 enum lttng_action_type type
,
57 action_validate_cb validate
,
58 action_serialize_cb serialize
,
59 action_equal_cb equal
,
60 action_destroy_cb destroy
)
62 urcu_ref_init(&action
->ref
);
64 action
->validate
= validate
;
65 action
->serialize
= serialize
;
66 action
->equal
= equal
;
67 action
->destroy
= destroy
;
71 void action_destroy_ref(struct urcu_ref
*ref
)
73 struct lttng_action
*action
=
74 container_of(ref
, struct lttng_action
, ref
);
76 action
->destroy(action
);
80 void lttng_action_get(struct lttng_action
*action
)
82 urcu_ref_get(&action
->ref
);
86 void lttng_action_put(struct lttng_action
*action
)
92 assert(action
->destroy
);
93 urcu_ref_put(&action
->ref
, action_destroy_ref
);
96 void lttng_action_destroy(struct lttng_action
*action
)
98 lttng_action_put(action
);
102 bool lttng_action_validate(struct lttng_action
*action
)
111 if (!action
->validate
) {
112 /* Sub-class guarantees that it can never be invalid. */
117 valid
= action
->validate(action
);
123 int lttng_action_serialize(struct lttng_action
*action
,
124 struct lttng_payload
*payload
)
127 struct lttng_action_comm action_comm
= {
128 .action_type
= (int8_t) action
->type
,
131 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &action_comm
,
132 sizeof(action_comm
));
137 ret
= action
->serialize(action
, payload
);
146 ssize_t
lttng_action_create_from_payload(struct lttng_payload_view
*view
,
147 struct lttng_action
**action
)
149 ssize_t consumed_len
, specific_action_consumed_len
;
150 const struct lttng_action_comm
*action_comm
;
151 action_create_from_payload_cb create_from_payload_cb
;
153 if (!view
|| !action
) {
158 action_comm
= (const struct lttng_action_comm
*) view
->buffer
.data
;
160 DBG("Create action from payload: action-type=%s",
161 lttng_action_type_string(action_comm
->action_type
));
163 switch (action_comm
->action_type
) {
164 case LTTNG_ACTION_TYPE_NOTIFY
:
165 create_from_payload_cb
= lttng_action_notify_create_from_payload
;
167 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
168 create_from_payload_cb
=
169 lttng_action_rotate_session_create_from_payload
;
171 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
172 create_from_payload_cb
=
173 lttng_action_snapshot_session_create_from_payload
;
175 case LTTNG_ACTION_TYPE_START_SESSION
:
176 create_from_payload_cb
=
177 lttng_action_start_session_create_from_payload
;
179 case LTTNG_ACTION_TYPE_STOP_SESSION
:
180 create_from_payload_cb
=
181 lttng_action_stop_session_create_from_payload
;
183 case LTTNG_ACTION_TYPE_GROUP
:
184 create_from_payload_cb
= lttng_action_group_create_from_payload
;
187 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
188 action_comm
->action_type
,
189 lttng_action_type_string(
190 action_comm
->action_type
));
196 /* Create buffer view for the action-type-specific data. */
197 struct lttng_payload_view specific_action_view
=
198 lttng_payload_view_from_view(view
,
199 sizeof(struct lttng_action_comm
),
202 specific_action_consumed_len
= create_from_payload_cb(
203 &specific_action_view
, action
);
205 if (specific_action_consumed_len
< 0) {
206 ERR("Failed to create specific action from buffer.");
213 consumed_len
= sizeof(struct lttng_action_comm
) +
214 specific_action_consumed_len
;
221 bool lttng_action_is_equal(const struct lttng_action
*a
,
222 const struct lttng_action
*b
)
224 bool is_equal
= false;
230 if (a
->type
!= b
->type
) {
240 is_equal
= a
->equal(a
, b
);