sessiond: ust: conditionally enable the underscore prefix variant quirk
[lttng-tools.git] / src / common / actions / start-session.cpp
1 /*
2 * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/error.hpp>
9 #include <common/macros.hpp>
10 #include <common/mi-lttng.hpp>
11 #include <lttng/action/action-internal.hpp>
12 #include <lttng/action/rate-policy-internal.hpp>
13 #include <lttng/action/rate-policy.h>
14 #include <lttng/action/start-session-internal.hpp>
15 #include <lttng/action/start-session.h>
16
17 #define IS_START_SESSION_ACTION(action) \
18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
19
20 namespace {
21 struct lttng_action_start_session {
22 struct lttng_action parent;
23
24 /* Owned by this. */
25 char *session_name;
26 struct lttng_rate_policy *policy;
27 };
28
29 struct lttng_action_start_session_comm {
30 /* Includes the trailing \0. */
31 uint32_t session_name_len;
32
33 /*
34 * Variable data:
35 *
36 * - session name (null terminated)
37 * - policy
38 */
39 char data[];
40 } LTTNG_PACKED;
41 } /* namespace */
42
43 static const struct lttng_rate_policy *
44 lttng_action_start_session_internal_get_rate_policy(
45 const struct lttng_action *action);
46
47 static struct lttng_action_start_session *action_start_session_from_action(
48 struct lttng_action *action)
49 {
50 LTTNG_ASSERT(action);
51
52 return lttng::utils::container_of(action, &lttng_action_start_session::parent);
53 }
54
55 static const struct lttng_action_start_session *
56 action_start_session_from_action_const(const struct lttng_action *action)
57 {
58 LTTNG_ASSERT(action);
59
60 return lttng::utils::container_of(action, &lttng_action_start_session::parent);
61 }
62
63 static bool lttng_action_start_session_validate(struct lttng_action *action)
64 {
65 bool valid;
66 struct lttng_action_start_session *action_start_session;
67
68 if (!action) {
69 valid = false;
70 goto end;
71 }
72
73 action_start_session = action_start_session_from_action(action);
74
75 /* A non-empty session name is mandatory. */
76 if (!action_start_session->session_name ||
77 strlen(action_start_session->session_name) == 0) {
78 valid = false;
79 goto end;
80 }
81
82 valid = true;
83 end:
84 return valid;
85 }
86
87 static bool lttng_action_start_session_is_equal(
88 const struct lttng_action *_a, const struct lttng_action *_b)
89 {
90 bool is_equal = false;
91 struct lttng_action_start_session *a, *b;
92
93 a = lttng::utils::container_of(_a, &lttng_action_start_session::parent);
94 b = lttng::utils::container_of(_b, &lttng_action_start_session::parent);
95
96 /* Action is not valid if this is not true. */
97 LTTNG_ASSERT(a->session_name);
98 LTTNG_ASSERT(b->session_name);
99 if (strcmp(a->session_name, b->session_name)) {
100 goto end;
101 }
102
103 is_equal = lttng_rate_policy_is_equal(a->policy, b->policy);
104 end:
105 return is_equal;
106 }
107
108 static int lttng_action_start_session_serialize(
109 struct lttng_action *action, struct lttng_payload *payload)
110 {
111 struct lttng_action_start_session *action_start_session;
112 struct lttng_action_start_session_comm comm;
113 size_t session_name_len;
114 int ret;
115
116 LTTNG_ASSERT(action);
117 LTTNG_ASSERT(payload);
118
119 action_start_session = action_start_session_from_action(action);
120
121 LTTNG_ASSERT(action_start_session->session_name);
122
123 DBG("Serializing start session action: session-name: %s",
124 action_start_session->session_name);
125
126 session_name_len = strlen(action_start_session->session_name) + 1;
127 comm.session_name_len = session_name_len;
128
129 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
130 if (ret) {
131 ret = -1;
132 goto end;
133 }
134
135 ret = lttng_dynamic_buffer_append(&payload->buffer,
136 action_start_session->session_name, session_name_len);
137 if (ret) {
138 ret = -1;
139 goto end;
140 }
141
142 ret = lttng_rate_policy_serialize(
143 action_start_session->policy, payload);
144 if (ret) {
145 ret = -1;
146 goto end;
147 }
148
149 ret = 0;
150 end:
151 return ret;
152 }
153
154 static void lttng_action_start_session_destroy(struct lttng_action *action)
155 {
156 struct lttng_action_start_session *action_start_session;
157
158 if (!action) {
159 goto end;
160 }
161
162 action_start_session = action_start_session_from_action(action);
163
164 lttng_rate_policy_destroy(action_start_session->policy);
165 free(action_start_session->session_name);
166 free(action_start_session);
167
168 end:
169 return;
170 }
171
172 ssize_t lttng_action_start_session_create_from_payload(
173 struct lttng_payload_view *view,
174 struct lttng_action **p_action)
175 {
176 ssize_t consumed_len, ret;
177 const struct lttng_action_start_session_comm *comm;
178 const char *session_name;
179 struct lttng_action *action = NULL;
180 enum lttng_action_status status;
181 struct lttng_rate_policy *policy = NULL;
182
183 comm = (typeof(comm)) view->buffer.data;
184 session_name = (const char *) &comm->data;
185
186 /* Session name */
187 if (!lttng_buffer_view_contains_string(&view->buffer, session_name,
188 comm->session_name_len)) {
189 consumed_len = -1;
190 goto end;
191 }
192 consumed_len = sizeof(*comm) + comm->session_name_len;
193
194 /* Rate policy. */
195 {
196 struct lttng_payload_view policy_view =
197 lttng_payload_view_from_view(
198 view, consumed_len, -1);
199 ret = lttng_rate_policy_create_from_payload(
200 &policy_view, &policy);
201 if (ret < 0) {
202 consumed_len = -1;
203 goto end;
204 }
205 consumed_len += ret;
206 }
207
208 action = lttng_action_start_session_create();
209 if (!action) {
210 consumed_len = -1;
211 goto end;
212 }
213
214 status = lttng_action_start_session_set_session_name(
215 action, session_name);
216 if (status != LTTNG_ACTION_STATUS_OK) {
217 consumed_len = -1;
218 goto end;
219 }
220
221 LTTNG_ASSERT(policy);
222 status = lttng_action_start_session_set_rate_policy(action, policy);
223 if (status != LTTNG_ACTION_STATUS_OK) {
224 consumed_len = -1;
225 goto end;
226 }
227
228 *p_action = action;
229 action = NULL;
230
231 end:
232 lttng_rate_policy_destroy(policy);
233 lttng_action_start_session_destroy(action);
234
235 return consumed_len;
236 }
237
238 static enum lttng_error_code lttng_action_start_session_mi_serialize(
239 const struct lttng_action *action, struct mi_writer *writer)
240 {
241 int ret;
242 enum lttng_error_code ret_code;
243 enum lttng_action_status status;
244 const char *session_name = NULL;
245 const struct lttng_rate_policy *policy = NULL;
246
247 LTTNG_ASSERT(action);
248 LTTNG_ASSERT(IS_START_SESSION_ACTION(action));
249
250 status = lttng_action_start_session_get_session_name(
251 action, &session_name);
252 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
253 LTTNG_ASSERT(session_name != NULL);
254
255 status = lttng_action_start_session_get_rate_policy(action, &policy);
256 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
257 LTTNG_ASSERT(policy != NULL);
258
259 /* Open action start session element. */
260 ret = mi_lttng_writer_open_element(
261 writer, mi_lttng_element_action_start_session);
262 if (ret) {
263 goto mi_error;
264 }
265
266 /* Session name. */
267 ret = mi_lttng_writer_write_element_string(
268 writer, mi_lttng_element_session_name, session_name);
269 if (ret) {
270 goto mi_error;
271 }
272
273 /* Rate policy. */
274 ret_code = lttng_rate_policy_mi_serialize(policy, writer);
275 if (ret_code != LTTNG_OK) {
276 goto end;
277 }
278
279 /* Close action start session element. */
280 ret = mi_lttng_writer_close_element(writer);
281 if (ret) {
282 goto mi_error;
283 }
284
285 ret_code = LTTNG_OK;
286 goto end;
287
288 mi_error:
289 ret_code = LTTNG_ERR_MI_IO_FAIL;
290 end:
291 return ret_code;
292 }
293
294 struct lttng_action *lttng_action_start_session_create(void)
295 {
296 struct lttng_action_start_session *action_start = NULL;
297 struct lttng_rate_policy *policy = NULL;
298 enum lttng_action_status status;
299
300 /* Create a every N = 1 rate policy. */
301 policy = lttng_rate_policy_every_n_create(1);
302 if (!policy) {
303 goto end;
304 }
305
306 action_start = zmalloc<lttng_action_start_session>();
307 if (!action_start) {
308 goto end;
309 }
310
311 lttng_action_init(&action_start->parent,
312 LTTNG_ACTION_TYPE_START_SESSION,
313 lttng_action_start_session_validate,
314 lttng_action_start_session_serialize,
315 lttng_action_start_session_is_equal,
316 lttng_action_start_session_destroy,
317 lttng_action_start_session_internal_get_rate_policy,
318 lttng_action_generic_add_error_query_results,
319 lttng_action_start_session_mi_serialize);
320
321 status = lttng_action_start_session_set_rate_policy(
322 &action_start->parent, policy);
323 if (status != LTTNG_ACTION_STATUS_OK) {
324 lttng_action_destroy(&action_start->parent);
325 action_start = NULL;
326 goto end;
327 }
328
329 end:
330 lttng_rate_policy_destroy(policy);
331 return &action_start->parent;
332 }
333
334 enum lttng_action_status lttng_action_start_session_set_session_name(
335 struct lttng_action *action, const char *session_name)
336 {
337 struct lttng_action_start_session *action_start_session;
338 enum lttng_action_status status;
339
340 if (!action || !IS_START_SESSION_ACTION(action) || !session_name ||
341 strlen(session_name) == 0) {
342 status = LTTNG_ACTION_STATUS_INVALID;
343 goto end;
344 }
345
346 action_start_session = action_start_session_from_action(action);
347
348 free(action_start_session->session_name);
349
350 action_start_session->session_name = strdup(session_name);
351 if (!action_start_session->session_name) {
352 status = LTTNG_ACTION_STATUS_ERROR;
353 goto end;
354 }
355
356 status = LTTNG_ACTION_STATUS_OK;
357 end:
358 return status;
359 }
360
361 enum lttng_action_status lttng_action_start_session_get_session_name(
362 const struct lttng_action *action, const char **session_name)
363 {
364 const struct lttng_action_start_session *action_start_session;
365 enum lttng_action_status status;
366
367 if (!action || !IS_START_SESSION_ACTION(action) || !session_name) {
368 status = LTTNG_ACTION_STATUS_INVALID;
369 goto end;
370 }
371
372 action_start_session = action_start_session_from_action_const(action);
373
374 *session_name = action_start_session->session_name;
375
376 status = LTTNG_ACTION_STATUS_OK;
377 end:
378 return status;
379 }
380
381 enum lttng_action_status lttng_action_start_session_set_rate_policy(
382 struct lttng_action *action,
383 const struct lttng_rate_policy *policy)
384 {
385 enum lttng_action_status status;
386 struct lttng_action_start_session *start_session_action;
387 struct lttng_rate_policy *copy = NULL;
388
389 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
390 status = LTTNG_ACTION_STATUS_INVALID;
391 goto end;
392 }
393
394 copy = lttng_rate_policy_copy(policy);
395 if (!copy) {
396 status = LTTNG_ACTION_STATUS_ERROR;
397 goto end;
398 }
399
400 start_session_action = action_start_session_from_action(action);
401
402 /* Release the previous rate policy .*/
403 lttng_rate_policy_destroy(start_session_action->policy);
404
405 /* Assign the policy. */
406 start_session_action->policy = copy;
407 status = LTTNG_ACTION_STATUS_OK;
408 copy = NULL;
409
410 end:
411 lttng_rate_policy_destroy(copy);
412 return status;
413 }
414
415 enum lttng_action_status lttng_action_start_session_get_rate_policy(
416 const struct lttng_action *action,
417 const struct lttng_rate_policy **policy)
418 {
419 enum lttng_action_status status;
420 const struct lttng_action_start_session *start_session_action;
421
422 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
423 status = LTTNG_ACTION_STATUS_INVALID;
424 goto end;
425 }
426
427 start_session_action = action_start_session_from_action_const(action);
428
429 *policy = start_session_action->policy;
430 status = LTTNG_ACTION_STATUS_OK;
431 end:
432 return status;
433 }
434
435 static const struct lttng_rate_policy *
436 lttng_action_start_session_internal_get_rate_policy(
437 const struct lttng_action *action)
438 {
439 const struct lttng_action_start_session *_action;
440 _action = action_start_session_from_action_const(action);
441
442 return _action->policy;
443 }
This page took 0.037699 seconds and 4 git commands to generate.