Introduce lttng_domain_type_str utility
[lttng-tools.git] / src / common / actions / rotate-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/rotate-session-internal.h>
13 #include <lttng/action/rotate-session.h>
14
15 #define IS_ROTATE_SESSION_ACTION(action) \
16 (lttng_action_get_type_const(action) == LTTNG_ACTION_TYPE_ROTATE_SESSION)
17
18 struct lttng_action_rotate_session {
19 struct lttng_action parent;
20
21 /* Owned by this. */
22 char *session_name;
23 };
24
25 struct lttng_action_rotate_session_comm {
26 /* Includes the trailing \0. */
27 uint32_t session_name_len;
28
29 /*
30 * Variable data:
31 *
32 * - session name (null terminated)
33 */
34 char data[];
35 } LTTNG_PACKED;
36
37 static struct lttng_action_rotate_session *action_rotate_session_from_action(
38 struct lttng_action *action)
39 {
40 assert(action);
41
42 return container_of(action, struct lttng_action_rotate_session, parent);
43 }
44
45 static const struct lttng_action_rotate_session *
46 action_rotate_session_from_action_const(const struct lttng_action *action)
47 {
48 assert(action);
49
50 return container_of(action, struct lttng_action_rotate_session, parent);
51 }
52
53 static bool lttng_action_rotate_session_validate(struct lttng_action *action)
54 {
55 bool valid;
56 struct lttng_action_rotate_session *action_rotate_session;
57
58 if (!action) {
59 valid = false;
60 goto end;
61 }
62
63 action_rotate_session = action_rotate_session_from_action(action);
64
65 /* A non-empty session name is mandatory. */
66 if (!action_rotate_session->session_name ||
67 strlen(action_rotate_session->session_name) == 0) {
68 valid = false;
69 goto end;
70 }
71
72 valid = true;
73 end:
74 return valid;
75 }
76
77 static bool lttng_action_rotate_session_is_equal(
78 const struct lttng_action *_a, const struct lttng_action *_b)
79 {
80 bool is_equal = false;
81 const struct lttng_action_rotate_session *a, *b;
82
83 a = action_rotate_session_from_action_const(_a);
84 b = action_rotate_session_from_action_const(_b);
85
86 /* Action is not valid if this is not true. */
87 assert(a->session_name);
88 assert(b->session_name);
89 if (strcmp(a->session_name, b->session_name)) {
90 goto end;
91 }
92
93 is_equal = true;
94 end:
95 return is_equal;
96 }
97 static int lttng_action_rotate_session_serialize(
98 struct lttng_action *action, struct lttng_payload *payload)
99 {
100 struct lttng_action_rotate_session *action_rotate_session;
101 struct lttng_action_rotate_session_comm comm;
102 size_t session_name_len;
103 int ret;
104
105 assert(action);
106 assert(payload);
107
108 action_rotate_session = action_rotate_session_from_action(action);
109
110 assert(action_rotate_session->session_name);
111
112 DBG("Serializing rotate session action: session-name: %s",
113 action_rotate_session->session_name);
114
115 session_name_len = strlen(action_rotate_session->session_name) + 1;
116 comm.session_name_len = session_name_len;
117
118 ret = lttng_dynamic_buffer_append(
119 &payload->buffer, &comm, sizeof(comm));
120 if (ret) {
121 ret = -1;
122 goto end;
123 }
124
125 ret = lttng_dynamic_buffer_append(&payload->buffer,
126 action_rotate_session->session_name, session_name_len);
127 if (ret) {
128 ret = -1;
129 goto end;
130 }
131
132 ret = 0;
133 end:
134 return ret;
135 }
136
137 static void lttng_action_rotate_session_destroy(struct lttng_action *action)
138 {
139 struct lttng_action_rotate_session *action_rotate_session;
140
141 if (!action) {
142 goto end;
143 }
144
145 action_rotate_session = action_rotate_session_from_action(action);
146
147 free(action_rotate_session->session_name);
148 free(action_rotate_session);
149
150 end:
151 return;
152 }
153
154 ssize_t lttng_action_rotate_session_create_from_payload(
155 struct lttng_payload_view *view,
156 struct lttng_action **p_action)
157 {
158 ssize_t consumed_len;
159 const struct lttng_action_rotate_session_comm *comm;
160 const char *session_name;
161 struct lttng_action *action;
162 enum lttng_action_status status;
163
164 action = lttng_action_rotate_session_create();
165 if (!action) {
166 consumed_len = -1;
167 goto end;
168 }
169
170 comm = (typeof(comm)) view->buffer.data;
171 session_name = (const char *) &comm->data;
172
173 if (!lttng_buffer_view_contains_string(
174 &view->buffer, session_name, comm->session_name_len)) {
175 consumed_len = -1;
176 goto end;
177 }
178
179 status = lttng_action_rotate_session_set_session_name(
180 action, session_name);
181 if (status != LTTNG_ACTION_STATUS_OK) {
182 consumed_len = -1;
183 goto end;
184 }
185
186 consumed_len = sizeof(*comm) + comm->session_name_len;
187 *p_action = action;
188 action = NULL;
189
190 end:
191 lttng_action_rotate_session_destroy(action);
192
193 return consumed_len;
194 }
195
196 struct lttng_action *lttng_action_rotate_session_create(void)
197 {
198 struct lttng_action *action;
199
200 action = zmalloc(sizeof(struct lttng_action_rotate_session));
201 if (!action) {
202 goto end;
203 }
204
205 lttng_action_init(action, LTTNG_ACTION_TYPE_ROTATE_SESSION,
206 lttng_action_rotate_session_validate,
207 lttng_action_rotate_session_serialize,
208 lttng_action_rotate_session_is_equal,
209 lttng_action_rotate_session_destroy);
210
211 end:
212 return action;
213 }
214
215 enum lttng_action_status lttng_action_rotate_session_set_session_name(
216 struct lttng_action *action, const char *session_name)
217 {
218 struct lttng_action_rotate_session *action_rotate_session;
219 enum lttng_action_status status;
220
221 if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name ||
222 strlen(session_name) == 0) {
223 status = LTTNG_ACTION_STATUS_INVALID;
224 goto end;
225 }
226
227 action_rotate_session = action_rotate_session_from_action(action);
228
229 free(action_rotate_session->session_name);
230
231 action_rotate_session->session_name = strdup(session_name);
232 if (!action_rotate_session->session_name) {
233 status = LTTNG_ACTION_STATUS_ERROR;
234 goto end;
235 }
236
237 status = LTTNG_ACTION_STATUS_OK;
238 end:
239 return status;
240 }
241
242 enum lttng_action_status lttng_action_rotate_session_get_session_name(
243 const struct lttng_action *action, const char **session_name)
244 {
245 const struct lttng_action_rotate_session *action_rotate_session;
246 enum lttng_action_status status;
247
248 if (!action || !IS_ROTATE_SESSION_ACTION(action) || !session_name) {
249 status = LTTNG_ACTION_STATUS_INVALID;
250 goto end;
251 }
252
253 action_rotate_session = action_rotate_session_from_action_const(action);
254
255 *session_name = action_rotate_session->session_name;
256
257 status = LTTNG_ACTION_STATUS_OK;
258 end:
259 return status;
260 }
This page took 0.036296 seconds and 4 git commands to generate.