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