Commit | Line | Data |
---|---|---|
5024c2ac JR |
1 | /* |
2 | <<<<<<< HEAD | |
3 | * Copyright (C) 2019 EfficiOS, Inc. | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this library; if not, write to the Free Software Foundation, | |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | ======= | |
18 | * Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com> | |
19 | * | |
20 | * SPDX-License-Identifier: LGPL-2.1-only | |
21 | * | |
22 | >>>>>>> 2e5f09656... actions: introduce stop session action | |
23 | */ | |
24 | ||
25 | #include <assert.h> | |
26 | #include <common/error.h> | |
27 | #include <common/macros.h> | |
28 | #include <lttng/action/action-internal.h> | |
29 | #include <lttng/action/stop-session-internal.h> | |
30 | #include <lttng/action/stop-session.h> | |
31 | ||
32 | struct lttng_action_stop_session { | |
33 | struct lttng_action parent; | |
34 | ||
35 | /* Owned by this. */ | |
36 | char *session_name; | |
37 | }; | |
38 | ||
39 | struct lttng_action_stop_session_comm { | |
40 | /* Includes the trailing \0. */ | |
41 | uint32_t session_name_len; | |
42 | ||
43 | /* | |
44 | * Variable data: | |
45 | * | |
46 | * - session name (null terminated) | |
47 | */ | |
48 | char data[]; | |
49 | } LTTNG_PACKED; | |
50 | ||
51 | static struct lttng_action_stop_session *action_stop_session_from_action( | |
52 | struct lttng_action *action) | |
53 | { | |
54 | assert(action); | |
55 | ||
56 | return container_of(action, struct lttng_action_stop_session, parent); | |
57 | } | |
58 | ||
59 | static const struct lttng_action_stop_session *action_stop_session_from_action_const( | |
60 | const struct lttng_action *action) | |
61 | { | |
62 | assert(action); | |
63 | ||
64 | return container_of(action, struct lttng_action_stop_session, parent); | |
65 | } | |
66 | ||
67 | static bool lttng_action_stop_session_validate(struct lttng_action *action) | |
68 | { | |
69 | bool valid; | |
70 | struct lttng_action_stop_session *action_stop_session; | |
71 | ||
72 | if (!action) { | |
73 | valid = false; | |
74 | goto end; | |
75 | } | |
76 | ||
77 | action_stop_session = action_stop_session_from_action(action); | |
78 | ||
79 | /* A non-empty session name is mandatory. */ | |
80 | if (!action_stop_session->session_name || | |
81 | strlen(action_stop_session->session_name) == 0) { | |
82 | valid = false; | |
83 | goto end; | |
84 | } | |
85 | ||
86 | valid = true; | |
87 | end: | |
88 | return valid; | |
89 | } | |
90 | ||
91 | static bool lttng_action_stop_session_is_equal(const struct lttng_action *_a, const struct lttng_action *_b) | |
92 | { | |
93 | bool is_equal = false; | |
94 | const struct lttng_action_stop_session *a, *b; | |
95 | ||
96 | a = action_stop_session_from_action_const(_a); | |
97 | b = action_stop_session_from_action_const(_b); | |
98 | ||
99 | /* Action is not valid if this is not true. */ | |
100 | assert(a->session_name); | |
101 | assert(b->session_name); | |
102 | if (strcmp(a->session_name, b->session_name)) { | |
103 | goto end; | |
104 | } | |
105 | ||
106 | is_equal = true; | |
107 | end: | |
108 | return is_equal; | |
109 | } | |
110 | ||
111 | static int lttng_action_stop_session_serialize( | |
112 | struct lttng_action *action, struct lttng_dynamic_buffer *buf) | |
113 | { | |
114 | struct lttng_action_stop_session *action_stop_session; | |
115 | struct lttng_action_stop_session_comm comm; | |
116 | size_t session_name_len; | |
117 | int ret; | |
118 | ||
119 | assert(action); | |
120 | assert(buf); | |
121 | ||
122 | action_stop_session = action_stop_session_from_action(action); | |
123 | ||
124 | assert(action_stop_session->session_name); | |
125 | ||
126 | DBG("Serializing stop session action: session-name: %s", | |
127 | action_stop_session->session_name); | |
128 | ||
129 | session_name_len = strlen(action_stop_session->session_name) + 1; | |
130 | comm.session_name_len = session_name_len; | |
131 | ||
132 | ret = lttng_dynamic_buffer_append(buf, &comm, sizeof(comm)); | |
133 | if (ret) { | |
134 | ret = -1; | |
135 | goto end; | |
136 | } | |
137 | ||
138 | ret = lttng_dynamic_buffer_append(buf, | |
139 | action_stop_session->session_name, session_name_len); | |
140 | if (ret) { | |
141 | ret = -1; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | ret = 0; | |
146 | end: | |
147 | return ret; | |
148 | } | |
149 | ||
150 | static void lttng_action_stop_session_destroy(struct lttng_action *action) | |
151 | { | |
152 | struct lttng_action_stop_session *action_stop_session; | |
153 | ||
154 | if (!action) { | |
155 | goto end; | |
156 | } | |
157 | ||
158 | action_stop_session = action_stop_session_from_action(action); | |
159 | ||
160 | free(action_stop_session->session_name); | |
161 | free(action_stop_session); | |
162 | ||
163 | end: | |
164 | return; | |
165 | } | |
166 | ||
167 | ssize_t lttng_action_stop_session_create_from_buffer( | |
168 | const struct lttng_buffer_view *view, | |
169 | struct lttng_action **p_action) | |
170 | { | |
171 | ssize_t consumed_len; | |
172 | struct lttng_action_stop_session_comm *comm; | |
173 | const char *session_name; | |
174 | struct lttng_action *action; | |
175 | enum lttng_action_status status; | |
176 | ||
177 | action = lttng_action_stop_session_create(); | |
178 | if (!action) { | |
179 | consumed_len = -1; | |
180 | goto end; | |
181 | } | |
182 | ||
183 | comm = (struct lttng_action_stop_session_comm *) view->data; | |
184 | session_name = (const char *) &comm->data; | |
185 | ||
186 | if (!lttng_buffer_view_contains_string( | |
187 | view, session_name, comm->session_name_len)) { | |
188 | consumed_len = -1; | |
189 | goto end; | |
190 | } | |
191 | ||
192 | status = lttng_action_stop_session_set_session_name( | |
193 | action, session_name); | |
194 | if (status != LTTNG_ACTION_STATUS_OK) { | |
195 | consumed_len = -1; | |
196 | goto end; | |
197 | } | |
198 | ||
199 | consumed_len = sizeof(struct lttng_action_stop_session_comm) + | |
200 | comm->session_name_len; | |
201 | *p_action = action; | |
202 | action = NULL; | |
203 | ||
204 | end: | |
205 | lttng_action_stop_session_destroy(action); | |
206 | ||
207 | return consumed_len; | |
208 | } | |
209 | ||
210 | struct lttng_action *lttng_action_stop_session_create(void) | |
211 | { | |
212 | struct lttng_action *action; | |
213 | ||
214 | action = zmalloc(sizeof(struct lttng_action_stop_session)); | |
215 | if (!action) { | |
216 | goto end; | |
217 | } | |
218 | ||
219 | lttng_action_init(action, LTTNG_ACTION_TYPE_STOP_SESSION, | |
220 | lttng_action_stop_session_validate, | |
221 | lttng_action_stop_session_serialize, | |
222 | lttng_action_stop_session_is_equal, | |
223 | lttng_action_stop_session_destroy); | |
224 | ||
225 | end: | |
226 | return action; | |
227 | } | |
228 | ||
229 | enum lttng_action_status lttng_action_stop_session_set_session_name( | |
230 | struct lttng_action *action, const char *session_name) | |
231 | { | |
232 | struct lttng_action_stop_session *action_stop_session; | |
233 | enum lttng_action_status status; | |
234 | ||
235 | if (!action || !session_name || strlen(session_name) == 0) { | |
236 | status = LTTNG_ACTION_STATUS_INVALID; | |
237 | goto end; | |
238 | } | |
239 | ||
240 | action_stop_session = action_stop_session_from_action(action); | |
241 | ||
242 | free(action_stop_session->session_name); | |
243 | ||
244 | action_stop_session->session_name = strdup(session_name); | |
245 | if (!action_stop_session->session_name) { | |
246 | status = LTTNG_ACTION_STATUS_ERROR; | |
247 | goto end; | |
248 | } | |
249 | ||
250 | status = LTTNG_ACTION_STATUS_OK; | |
251 | end: | |
252 | return status; | |
253 | } | |
254 | ||
255 | enum lttng_action_status lttng_action_stop_session_get_session_name( | |
256 | const struct lttng_action *action, const char **session_name) | |
257 | { | |
258 | const struct lttng_action_stop_session *action_stop_session; | |
259 | enum lttng_action_status status; | |
260 | ||
261 | if (!action || !session_name) { | |
262 | status = LTTNG_ACTION_STATUS_INVALID; | |
263 | goto end; | |
264 | } | |
265 | ||
266 | action_stop_session = action_stop_session_from_action_const(action); | |
267 | ||
268 | *session_name = action_stop_session->session_name; | |
269 | ||
270 | status = LTTNG_ACTION_STATUS_OK; | |
271 | end: | |
272 | return status; | |
273 | } |