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/start-session-internal.h>
13 #include <lttng/action/stop-session-internal.h>
15 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
17 switch (action_type
) {
18 case LTTNG_ACTION_TYPE_UNKNOWN
:
20 case LTTNG_ACTION_TYPE_NOTIFY
:
22 case LTTNG_ACTION_TYPE_START_SESSION
:
23 return "START_SESSION";
24 case LTTNG_ACTION_TYPE_STOP_SESSION
:
25 return "STOP_SESSION";
31 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
33 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
37 enum lttng_action_type
lttng_action_get_type_const(
38 const struct lttng_action
*action
)
44 void lttng_action_init(
45 struct lttng_action
*action
,
46 enum lttng_action_type type
,
47 action_validate_cb validate
,
48 action_serialize_cb serialize
,
49 action_equal_cb equal
,
50 action_destroy_cb destroy
)
53 action
->validate
= validate
;
54 action
->serialize
= serialize
;
55 action
->equal
= equal
;
56 action
->destroy
= destroy
;
59 void lttng_action_destroy(struct lttng_action
*action
)
65 assert(action
->destroy
);
66 action
->destroy(action
);
70 bool lttng_action_validate(struct lttng_action
*action
)
79 if (!action
->validate
) {
80 /* Sub-class guarantees that it can never be invalid. */
85 valid
= action
->validate(action
);
91 int lttng_action_serialize(struct lttng_action
*action
,
92 struct lttng_dynamic_buffer
*buf
)
95 struct lttng_action_comm action_comm
= {
96 .action_type
= (int8_t) action
->type
,
99 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
100 sizeof(action_comm
));
105 ret
= action
->serialize(action
, buf
);
114 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
115 struct lttng_action
**action
)
117 ssize_t consumed_len
, specific_action_consumed_len
;
118 const struct lttng_action_comm
*action_comm
;
119 action_create_from_buffer_cb create_from_buffer_cb
;
120 struct lttng_buffer_view specific_action_view
;
122 if (!view
|| !action
) {
127 action_comm
= (const struct lttng_action_comm
*) view
->data
;
129 DBG("Create action from buffer: action-type=%s",
130 lttng_action_type_string(action_comm
->action_type
));
132 switch (action_comm
->action_type
) {
133 case LTTNG_ACTION_TYPE_NOTIFY
:
134 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
136 case LTTNG_ACTION_TYPE_START_SESSION
:
137 create_from_buffer_cb
=
138 lttng_action_start_session_create_from_buffer
;
140 case LTTNG_ACTION_TYPE_STOP_SESSION
:
141 create_from_buffer_cb
=
142 lttng_action_stop_session_create_from_buffer
;
145 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
146 action_comm
->action_type
,
147 lttng_action_type_string(
148 action_comm
->action_type
));
153 /* Create buffer view for the action-type-specific data. */
154 specific_action_view
= lttng_buffer_view_from_view(view
,
155 sizeof(struct lttng_action_comm
),
156 view
->size
- sizeof(struct lttng_action_comm
));
158 specific_action_consumed_len
=
159 create_from_buffer_cb(&specific_action_view
, action
);
160 if (specific_action_consumed_len
< 0) {
161 ERR("Failed to create specific action from buffer.");
168 consumed_len
= sizeof(struct lttng_action_comm
) +
169 specific_action_consumed_len
;
176 bool lttng_action_is_equal(const struct lttng_action
*a
,
177 const struct lttng_action
*b
)
179 bool is_equal
= false;
185 if (a
->type
!= b
->type
) {
195 is_equal
= a
->equal(a
, b
);