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>
14 static const char *lttng_action_type_string(enum lttng_action_type action_type
)
16 switch (action_type
) {
17 case LTTNG_ACTION_TYPE_UNKNOWN
:
19 case LTTNG_ACTION_TYPE_NOTIFY
:
21 case LTTNG_ACTION_TYPE_START_SESSION
:
22 return "START_SESSION";
28 enum lttng_action_type
lttng_action_get_type(struct lttng_action
*action
)
30 return action
? action
->type
: LTTNG_ACTION_TYPE_UNKNOWN
;
34 enum lttng_action_type
lttng_action_get_type_const(
35 const struct lttng_action
*action
)
41 void lttng_action_init(
42 struct lttng_action
*action
,
43 enum lttng_action_type type
,
44 action_validate_cb validate
,
45 action_serialize_cb serialize
,
46 action_equal_cb equal
,
47 action_destroy_cb destroy
)
50 action
->validate
= validate
;
51 action
->serialize
= serialize
;
52 action
->equal
= equal
;
53 action
->destroy
= destroy
;
56 void lttng_action_destroy(struct lttng_action
*action
)
62 assert(action
->destroy
);
63 action
->destroy(action
);
67 bool lttng_action_validate(struct lttng_action
*action
)
76 if (!action
->validate
) {
77 /* Sub-class guarantees that it can never be invalid. */
82 valid
= action
->validate(action
);
88 int lttng_action_serialize(struct lttng_action
*action
,
89 struct lttng_dynamic_buffer
*buf
)
92 struct lttng_action_comm action_comm
= {
93 .action_type
= (int8_t) action
->type
,
96 ret
= lttng_dynamic_buffer_append(buf
, &action_comm
,
102 ret
= action
->serialize(action
, buf
);
111 ssize_t
lttng_action_create_from_buffer(const struct lttng_buffer_view
*view
,
112 struct lttng_action
**action
)
114 ssize_t consumed_len
, specific_action_consumed_len
;
115 const struct lttng_action_comm
*action_comm
;
116 action_create_from_buffer_cb create_from_buffer_cb
;
117 struct lttng_buffer_view specific_action_view
;
119 if (!view
|| !action
) {
124 action_comm
= (const struct lttng_action_comm
*) view
->data
;
126 DBG("Create action from buffer: action-type=%s",
127 lttng_action_type_string(action_comm
->action_type
));
129 switch (action_comm
->action_type
) {
130 case LTTNG_ACTION_TYPE_NOTIFY
:
131 create_from_buffer_cb
= lttng_action_notify_create_from_buffer
;
133 case LTTNG_ACTION_TYPE_START_SESSION
:
134 create_from_buffer_cb
=
135 lttng_action_start_session_create_from_buffer
;
138 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
139 action_comm
->action_type
,
140 lttng_action_type_string(
141 action_comm
->action_type
));
146 /* Create buffer view for the action-type-specific data. */
147 specific_action_view
= lttng_buffer_view_from_view(view
,
148 sizeof(struct lttng_action_comm
),
149 view
->size
- sizeof(struct lttng_action_comm
));
151 specific_action_consumed_len
=
152 create_from_buffer_cb(&specific_action_view
, action
);
153 if (specific_action_consumed_len
< 0) {
154 ERR("Failed to create specific action from buffer.");
161 consumed_len
= sizeof(struct lttng_action_comm
) +
162 specific_action_consumed_len
;
169 bool lttng_action_is_equal(const struct lttng_action
*a
,
170 const struct lttng_action
*b
)
172 bool is_equal
= false;
178 if (a
->type
!= b
->type
) {
188 is_equal
= a
->equal(a
, b
);