Implement firing policy for the start session action
[lttng-tools.git] / src / common / actions / start-session.c
CommitLineData
58397d0d
SM
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 <assert.h>
9#include <common/error.h>
10#include <common/macros.h>
11#include <lttng/action/action-internal.h>
1345b16b
JR
12#include <lttng/action/firing-policy-internal.h>
13#include <lttng/action/firing-policy.h>
58397d0d
SM
14#include <lttng/action/start-session-internal.h>
15#include <lttng/action/start-session.h>
16
17#define IS_START_SESSION_ACTION(action) \
17182cfd 18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_START_SESSION)
58397d0d
SM
19
20struct lttng_action_start_session {
21 struct lttng_action parent;
22
23 /* Owned by this. */
24 char *session_name;
1345b16b 25 struct lttng_firing_policy *policy;
58397d0d
SM
26};
27
28struct lttng_action_start_session_comm {
29 /* Includes the trailing \0. */
30 uint32_t session_name_len;
31
32 /*
33 * Variable data:
34 *
35 * - session name (null terminated)
1345b16b 36 * - policy
58397d0d
SM
37 */
38 char data[];
39} LTTNG_PACKED;
40
41static struct lttng_action_start_session *action_start_session_from_action(
42 struct lttng_action *action)
43{
44 assert(action);
45
46 return container_of(action, struct lttng_action_start_session, parent);
47}
48
49static const struct lttng_action_start_session *
50action_start_session_from_action_const(const struct lttng_action *action)
51{
52 assert(action);
53
54 return container_of(action, struct lttng_action_start_session, parent);
55}
56
57static bool lttng_action_start_session_validate(struct lttng_action *action)
58{
59 bool valid;
60 struct lttng_action_start_session *action_start_session;
61
62 if (!action) {
63 valid = false;
64 goto end;
65 }
66
67 action_start_session = action_start_session_from_action(action);
68
69 /* A non-empty session name is mandatory. */
70 if (!action_start_session->session_name ||
71 strlen(action_start_session->session_name) == 0) {
72 valid = false;
73 goto end;
74 }
75
76 valid = true;
77end:
78 return valid;
79}
80
81static bool lttng_action_start_session_is_equal(
82 const struct lttng_action *_a, const struct lttng_action *_b)
83{
84 bool is_equal = false;
85 struct lttng_action_start_session *a, *b;
86
87 a = container_of(_a, struct lttng_action_start_session, parent);
88 b = container_of(_b, struct lttng_action_start_session, parent);
89
90 /* Action is not valid if this is not true. */
91 assert(a->session_name);
92 assert(b->session_name);
93 if (strcmp(a->session_name, b->session_name)) {
94 goto end;
95 }
96
1345b16b 97 is_equal = lttng_firing_policy_is_equal(a->policy, b->policy);
58397d0d
SM
98end:
99 return is_equal;
100}
101
102static int lttng_action_start_session_serialize(
c0a66c84 103 struct lttng_action *action, struct lttng_payload *payload)
58397d0d
SM
104{
105 struct lttng_action_start_session *action_start_session;
106 struct lttng_action_start_session_comm comm;
107 size_t session_name_len;
108 int ret;
109
110 assert(action);
c0a66c84 111 assert(payload);
58397d0d
SM
112
113 action_start_session = action_start_session_from_action(action);
114
115 assert(action_start_session->session_name);
116
117 DBG("Serializing start session action: session-name: %s",
118 action_start_session->session_name);
119
120 session_name_len = strlen(action_start_session->session_name) + 1;
121 comm.session_name_len = session_name_len;
122
c0a66c84 123 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm, sizeof(comm));
58397d0d
SM
124 if (ret) {
125 ret = -1;
126 goto end;
127 }
128
c0a66c84 129 ret = lttng_dynamic_buffer_append(&payload->buffer,
58397d0d
SM
130 action_start_session->session_name, session_name_len);
131 if (ret) {
132 ret = -1;
133 goto end;
134 }
135
1345b16b
JR
136 ret = lttng_firing_policy_serialize(
137 action_start_session->policy, payload);
138 if (ret) {
139 ret = -1;
140 goto end;
141 }
142
58397d0d
SM
143 ret = 0;
144end:
145 return ret;
146}
147
148static void lttng_action_start_session_destroy(struct lttng_action *action)
149{
150 struct lttng_action_start_session *action_start_session;
151
152 if (!action) {
153 goto end;
154 }
155
156 action_start_session = action_start_session_from_action(action);
157
1345b16b 158 lttng_firing_policy_destroy(action_start_session->policy);
58397d0d
SM
159 free(action_start_session->session_name);
160 free(action_start_session);
161
162end:
163 return;
164}
165
c0a66c84
JG
166ssize_t lttng_action_start_session_create_from_payload(
167 struct lttng_payload_view *view,
58397d0d
SM
168 struct lttng_action **p_action)
169{
1345b16b 170 ssize_t consumed_len, ret;
58397d0d
SM
171 const struct lttng_action_start_session_comm *comm;
172 const char *session_name;
1345b16b 173 struct lttng_action *action = NULL;
58397d0d 174 enum lttng_action_status status;
1345b16b 175 struct lttng_firing_policy *policy = NULL;
58397d0d 176
c0a66c84 177 comm = (typeof(comm)) view->buffer.data;
58397d0d
SM
178 session_name = (const char *) &comm->data;
179
1345b16b 180 /* Session name */
c0a66c84
JG
181 if (!lttng_buffer_view_contains_string(&view->buffer, session_name,
182 comm->session_name_len)) {
58397d0d
SM
183 consumed_len = -1;
184 goto end;
185 }
1345b16b
JR
186 consumed_len = sizeof(*comm) + comm->session_name_len;
187
188 /* Firing policy. */
189 {
190 struct lttng_payload_view policy_view =
191 lttng_payload_view_from_view(
192 view, consumed_len, -1);
193 ret = lttng_firing_policy_create_from_payload(
194 &policy_view, &policy);
195 if (ret < 0) {
196 consumed_len = -1;
197 goto end;
198 }
199 consumed_len += ret;
200 }
201
202 action = lttng_action_start_session_create();
203 if (!action) {
204 consumed_len = -1;
205 goto end;
206 }
58397d0d
SM
207
208 status = lttng_action_start_session_set_session_name(
209 action, session_name);
210 if (status != LTTNG_ACTION_STATUS_OK) {
211 consumed_len = -1;
212 goto end;
213 }
214
1345b16b
JR
215 assert(policy);
216 status = lttng_action_start_session_set_firing_policy(action, policy);
217 if (status != LTTNG_ACTION_STATUS_OK) {
218 consumed_len = -1;
219 goto end;
220 }
221
58397d0d
SM
222 *p_action = action;
223 action = NULL;
224
225end:
1345b16b 226 lttng_firing_policy_destroy(policy);
58397d0d
SM
227 lttng_action_start_session_destroy(action);
228
229 return consumed_len;
230}
231
232struct lttng_action *lttng_action_start_session_create(void)
233{
1345b16b
JR
234 struct lttng_action *action = NULL;
235 struct lttng_firing_policy *policy = NULL;
236 enum lttng_action_status status;
237
238 /* Create a every N = 1 firing policy. */
239 policy = lttng_firing_policy_every_n_create(1);
240 if (!policy) {
241 goto end;
242 }
58397d0d
SM
243
244 action = zmalloc(sizeof(struct lttng_action_start_session));
245 if (!action) {
246 goto end;
247 }
248
249 lttng_action_init(action, LTTNG_ACTION_TYPE_START_SESSION,
250 lttng_action_start_session_validate,
251 lttng_action_start_session_serialize,
252 lttng_action_start_session_is_equal,
253 lttng_action_start_session_destroy);
254
1345b16b
JR
255 status = lttng_action_start_session_set_firing_policy(action, policy);
256 if (status != LTTNG_ACTION_STATUS_OK) {
257 free(action);
258 action = NULL;
259 goto end;
260 }
261
58397d0d 262end:
1345b16b 263 lttng_firing_policy_destroy(policy);
58397d0d
SM
264 return action;
265}
266
267enum lttng_action_status lttng_action_start_session_set_session_name(
268 struct lttng_action *action, const char *session_name)
269{
270 struct lttng_action_start_session *action_start_session;
271 enum lttng_action_status status;
272
273 if (!action || !IS_START_SESSION_ACTION(action) || !session_name ||
274 strlen(session_name) == 0) {
275 status = LTTNG_ACTION_STATUS_INVALID;
276 goto end;
277 }
278
279 action_start_session = action_start_session_from_action(action);
280
281 free(action_start_session->session_name);
282
283 action_start_session->session_name = strdup(session_name);
284 if (!action_start_session->session_name) {
285 status = LTTNG_ACTION_STATUS_ERROR;
286 goto end;
287 }
288
289 status = LTTNG_ACTION_STATUS_OK;
290end:
291 return status;
292}
293
294enum lttng_action_status lttng_action_start_session_get_session_name(
295 const struct lttng_action *action, const char **session_name)
296{
297 const struct lttng_action_start_session *action_start_session;
298 enum lttng_action_status status;
299
300 if (!action || !IS_START_SESSION_ACTION(action) || !session_name) {
301 status = LTTNG_ACTION_STATUS_INVALID;
302 goto end;
303 }
304
305 action_start_session = action_start_session_from_action_const(action);
306
307 *session_name = action_start_session->session_name;
308
309 status = LTTNG_ACTION_STATUS_OK;
310end:
311 return status;
312}
1345b16b
JR
313
314enum lttng_action_status lttng_action_start_session_set_firing_policy(
315 struct lttng_action *action,
316 const struct lttng_firing_policy *policy)
317{
318 enum lttng_action_status status;
319 struct lttng_action_start_session *start_session_action;
320 struct lttng_firing_policy *copy = NULL;
321
322 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
323 status = LTTNG_ACTION_STATUS_INVALID;
324 goto end;
325 }
326
327 copy = lttng_firing_policy_copy(policy);
328 if (!copy) {
329 status = LTTNG_ACTION_STATUS_ERROR;
330 goto end;
331 }
332
333 start_session_action = action_start_session_from_action(action);
334
335 /* Release the previous firing policy .*/
336 lttng_firing_policy_destroy(start_session_action->policy);
337
338 /* Assign the policy. */
339 start_session_action->policy = copy;
340 status = LTTNG_ACTION_STATUS_OK;
341 copy = NULL;
342
343end:
344 lttng_firing_policy_destroy(copy);
345 return status;
346}
347
348enum lttng_action_status lttng_action_start_session_get_firing_policy(
349 const struct lttng_action *action,
350 const struct lttng_firing_policy **policy)
351{
352 enum lttng_action_status status;
353 const struct lttng_action_start_session *start_session_action;
354
355 if (!action || !policy || !IS_START_SESSION_ACTION(action)) {
356 status = LTTNG_ACTION_STATUS_INVALID;
357 goto end;
358 }
359
360 start_session_action = action_start_session_from_action_const(action);
361
362 *policy = start_session_action->policy;
363 status = LTTNG_ACTION_STATUS_OK;
364end:
365 return status;
366}
This page took 0.059071 seconds and 4 git commands to generate.