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/start-session-internal.h>
14 #include <lttng/action/stop-session-internal.h>
16 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
18 switch (action_type
) {
19 case LTTNG_ACTION_TYPE_UNKNOWN
:
21 case LTTNG_ACTION_TYPE_NOTIFY
:
23 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
24 return "ROTATE_SESSION";
25 case LTTNG_ACTION_TYPE_START_SESSION
:
26 return "START_SESSION";
27 case LTTNG_ACTION_TYPE_STOP_SESSION
:
28 return "STOP_SESSION";
34 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
36 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
40 enum lttng_action_type
lttng_action_get_type_const(
41 const struct lttng_action
*action
)
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
)
56 action
->validate
= validate
;
57 action
->serialize
= serialize
;
58 action
->equal
= equal
;
59 action
->destroy
= destroy
;
62 void lttng_action_destroy(struct lttng_action
*action
)
68 assert(action
->destroy
);
69 action
->destroy(action
);
73 bool lttng_action_validate(struct lttng_action
*action
)
82 if (!action
->validate
) {
83 /* Sub-class guarantees that it can never be invalid. */
88 valid
= action
->validate(action
);
94 int lttng_action_serialize(struct lttng_action
*action
,
95 struct lttng_dynamic_buffer
*buf
)
98 struct lttng_action_comm action_comm
= {
99 .action_type
= (int8_t) action
->type
,
102 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
103 sizeof(action_comm
));
108 ret
= action
->serialize(action
, buf
);
117 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
118 struct lttng_action
**action
)
120 ssize_t consumed_len
, specific_action_consumed_len
;
121 const struct lttng_action_comm
*action_comm
;
122 action_create_from_buffer_cb create_from_buffer_cb
;
123 struct lttng_buffer_view specific_action_view
;
125 if (!view
|| !action
) {
130 action_comm
= (const struct lttng_action_comm
*) view
->data
;
132 DBG("Create action from buffer: action-type=%s",
133 lttng_action_type_string(action_comm
->action_type
));
135 switch (action_comm
->action_type
) {
136 case LTTNG_ACTION_TYPE_NOTIFY
:
137 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
139 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
140 create_from_buffer_cb
=
141 lttng_action_rotate_session_create_from_buffer
;
143 case LTTNG_ACTION_TYPE_START_SESSION
:
144 create_from_buffer_cb
=
145 lttng_action_start_session_create_from_buffer
;
147 case LTTNG_ACTION_TYPE_STOP_SESSION
:
148 create_from_buffer_cb
=
149 lttng_action_stop_session_create_from_buffer
;
152 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
153 action_comm
->action_type
,
154 lttng_action_type_string(
155 action_comm
->action_type
));
160 /* Create buffer view for the action-type-specific data. */
161 specific_action_view
= lttng_buffer_view_from_view(view
,
162 sizeof(struct lttng_action_comm
),
163 view
->size
- sizeof(struct lttng_action_comm
));
165 specific_action_consumed_len
=
166 create_from_buffer_cb(&specific_action_view
, action
);
167 if (specific_action_consumed_len
< 0) {
168 ERR("Failed to create specific action from buffer.");
175 consumed_len
= sizeof(struct lttng_action_comm
) +
176 specific_action_consumed_len
;
183 bool lttng_action_is_equal(const struct lttng_action
*a
,
184 const struct lttng_action
*b
)
186 bool is_equal
= false;
192 if (a
->type
!= b
->type
) {
202 is_equal
= a
->equal(a
, b
);