Commit | Line | Data |
---|---|---|
a58c490f | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
a58c490f | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
a58c490f | 5 | * |
a58c490f JG |
6 | */ |
7 | ||
5024c2ac JR |
8 | #include <assert.h> |
9 | #include <common/error.h> | |
a58c490f | 10 | #include <lttng/action/action-internal.h> |
5024c2ac | 11 | #include <lttng/action/group-internal.h> |
a58c490f | 12 | #include <lttng/action/notify-internal.h> |
5024c2ac JR |
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> | |
a58c490f | 17 | |
5024c2ac JR |
18 | LTTNG_HIDDEN |
19 | const char *lttng_action_type_string(enum lttng_action_type action_type) | |
2666d352 SM |
20 | { |
21 | switch (action_type) { | |
22 | case LTTNG_ACTION_TYPE_UNKNOWN: | |
23 | return "UNKNOWN"; | |
5024c2ac JR |
24 | case LTTNG_ACTION_TYPE_GROUP: |
25 | return "GROUP"; | |
2666d352 SM |
26 | case LTTNG_ACTION_TYPE_NOTIFY: |
27 | return "NOTIFY"; | |
5024c2ac JR |
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"; | |
2666d352 SM |
36 | default: |
37 | return "???"; | |
38 | } | |
39 | } | |
40 | ||
5024c2ac | 41 | enum lttng_action_type lttng_action_get_type(const struct lttng_action *action) |
a58c490f JG |
42 | { |
43 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; | |
44 | } | |
45 | ||
9b63a4aa JG |
46 | LTTNG_HIDDEN |
47 | enum lttng_action_type lttng_action_get_type_const( | |
48 | const struct lttng_action *action) | |
49 | { | |
50 | return action->type; | |
51 | } | |
52 | ||
6acb3f46 SM |
53 | LTTNG_HIDDEN |
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, | |
5024c2ac | 59 | action_equal_cb equal, |
6acb3f46 SM |
60 | action_destroy_cb destroy) |
61 | { | |
5024c2ac | 62 | urcu_ref_init(&action->ref); |
6acb3f46 SM |
63 | action->type = type; |
64 | action->validate = validate; | |
65 | action->serialize = serialize; | |
5024c2ac | 66 | action->equal = equal; |
6acb3f46 SM |
67 | action->destroy = destroy; |
68 | } | |
69 | ||
5024c2ac JR |
70 | static |
71 | void action_destroy_ref(struct urcu_ref *ref) | |
72 | { | |
73 | struct lttng_action *action = | |
74 | container_of(ref, struct lttng_action, ref); | |
75 | ||
76 | action->destroy(action); | |
77 | } | |
78 | ||
79 | LTTNG_HIDDEN | |
80 | void lttng_action_get(struct lttng_action *action) | |
81 | { | |
82 | urcu_ref_get(&action->ref); | |
83 | } | |
84 | ||
85 | LTTNG_HIDDEN | |
86 | void lttng_action_put(struct lttng_action *action) | |
a58c490f JG |
87 | { |
88 | if (!action) { | |
89 | return; | |
90 | } | |
91 | ||
92 | assert(action->destroy); | |
5024c2ac JR |
93 | urcu_ref_put(&action->ref, action_destroy_ref); |
94 | } | |
95 | ||
96 | void lttng_action_destroy(struct lttng_action *action) | |
97 | { | |
98 | lttng_action_put(action); | |
a58c490f JG |
99 | } |
100 | ||
101 | LTTNG_HIDDEN | |
102 | bool lttng_action_validate(struct lttng_action *action) | |
103 | { | |
104 | bool valid; | |
105 | ||
106 | if (!action) { | |
107 | valid = false; | |
108 | goto end; | |
109 | } | |
110 | ||
111 | if (!action->validate) { | |
112 | /* Sub-class guarantees that it can never be invalid. */ | |
113 | valid = true; | |
114 | goto end; | |
115 | } | |
116 | ||
117 | valid = action->validate(action); | |
118 | end: | |
119 | return valid; | |
120 | } | |
121 | ||
122 | LTTNG_HIDDEN | |
3647288f JG |
123 | int lttng_action_serialize(struct lttng_action *action, |
124 | struct lttng_dynamic_buffer *buf) | |
a58c490f | 125 | { |
3647288f JG |
126 | int ret; |
127 | struct lttng_action_comm action_comm = { | |
128 | .action_type = (int8_t) action->type, | |
129 | }; | |
130 | ||
131 | ret = lttng_dynamic_buffer_append(buf, &action_comm, | |
132 | sizeof(action_comm)); | |
133 | if (ret) { | |
a58c490f JG |
134 | goto end; |
135 | } | |
136 | ||
3647288f JG |
137 | ret = action->serialize(action, buf); |
138 | if (ret) { | |
a58c490f JG |
139 | goto end; |
140 | } | |
a58c490f JG |
141 | end: |
142 | return ret; | |
143 | } | |
144 | ||
145 | LTTNG_HIDDEN | |
146 | ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view, | |
869a3c2d | 147 | struct lttng_action **action) |
a58c490f | 148 | { |
869a3c2d | 149 | ssize_t consumed_len, specific_action_consumed_len; |
a58c490f | 150 | const struct lttng_action_comm *action_comm; |
869a3c2d SM |
151 | action_create_from_buffer_cb create_from_buffer_cb; |
152 | struct lttng_buffer_view specific_action_view; | |
a58c490f | 153 | |
869a3c2d SM |
154 | if (!view || !action) { |
155 | consumed_len = -1; | |
a58c490f JG |
156 | goto end; |
157 | } | |
158 | ||
159 | action_comm = (const struct lttng_action_comm *) view->data; | |
869a3c2d | 160 | |
2666d352 SM |
161 | DBG("Create action from buffer: action-type=%s", |
162 | lttng_action_type_string(action_comm->action_type)); | |
163 | ||
a58c490f JG |
164 | switch (action_comm->action_type) { |
165 | case LTTNG_ACTION_TYPE_NOTIFY: | |
869a3c2d | 166 | create_from_buffer_cb = lttng_action_notify_create_from_buffer; |
a58c490f | 167 | break; |
5024c2ac JR |
168 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
169 | create_from_buffer_cb = | |
170 | lttng_action_rotate_session_create_from_buffer; | |
171 | break; | |
172 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: | |
173 | create_from_buffer_cb = | |
174 | lttng_action_snapshot_session_create_from_buffer; | |
175 | break; | |
176 | case LTTNG_ACTION_TYPE_START_SESSION: | |
177 | create_from_buffer_cb = | |
178 | lttng_action_start_session_create_from_buffer; | |
179 | break; | |
180 | case LTTNG_ACTION_TYPE_STOP_SESSION: | |
181 | create_from_buffer_cb = | |
182 | lttng_action_stop_session_create_from_buffer; | |
183 | break; | |
184 | case LTTNG_ACTION_TYPE_GROUP: | |
185 | create_from_buffer_cb = lttng_action_group_create_from_buffer; | |
186 | break; | |
a58c490f | 187 | default: |
2666d352 SM |
188 | ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)", |
189 | action_comm->action_type, | |
190 | lttng_action_type_string( | |
191 | action_comm->action_type)); | |
869a3c2d | 192 | consumed_len = -1; |
a58c490f JG |
193 | goto end; |
194 | } | |
195 | ||
869a3c2d SM |
196 | /* Create buffer view for the action-type-specific data. */ |
197 | specific_action_view = lttng_buffer_view_from_view(view, | |
198 | sizeof(struct lttng_action_comm), | |
199 | view->size - sizeof(struct lttng_action_comm)); | |
200 | ||
201 | specific_action_consumed_len = | |
202 | create_from_buffer_cb(&specific_action_view, action); | |
203 | if (specific_action_consumed_len < 0) { | |
204 | ERR("Failed to create specific action from buffer."); | |
205 | consumed_len = -1; | |
a58c490f JG |
206 | goto end; |
207 | } | |
869a3c2d SM |
208 | |
209 | assert(*action); | |
210 | ||
211 | consumed_len = sizeof(struct lttng_action_comm) + | |
212 | specific_action_consumed_len; | |
213 | ||
a58c490f | 214 | end: |
869a3c2d | 215 | return consumed_len; |
a58c490f | 216 | } |
5024c2ac JR |
217 | |
218 | LTTNG_HIDDEN | |
219 | bool lttng_action_is_equal(const struct lttng_action *a, | |
220 | const struct lttng_action *b) | |
221 | { | |
222 | bool is_equal = false; | |
223 | ||
224 | if (!a || !b) { | |
225 | goto end; | |
226 | } | |
227 | ||
228 | if (a->type != b->type) { | |
229 | goto end; | |
230 | } | |
231 | ||
232 | if (a == b) { | |
233 | is_equal = true; | |
234 | goto end; | |
235 | } | |
236 | ||
237 | is_equal = a->equal ? a->equal(a, b) : true; | |
238 | end: | |
239 | return is_equal; | |
240 | } | |
241 |