SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-commands.c
CommitLineData
ab0ee2ca 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
ab0ee2ca 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
ab0ee2ca 5 *
ab0ee2ca
JG
6 */
7
8#include <lttng/trigger/trigger.h>
9#include <lttng/lttng-error.h>
10#include "notification-thread.h"
11#include "notification-thread-commands.h"
12#include <common/error.h>
ab0ee2ca
JG
13#include <unistd.h>
14#include <stdint.h>
15#include <inttypes.h>
16
17static
18void init_notification_thread_command(struct notification_thread_command *cmd)
19{
ab0ee2ca 20 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
8ada111f 21 lttng_waiter_init(&cmd->reply_waiter);
ab0ee2ca
JG
22}
23
24static
25int run_command_wait(struct notification_thread_handle *handle,
26 struct notification_thread_command *cmd)
27{
28 int ret;
29 uint64_t notification_counter = 1;
30
ab0ee2ca
JG
31 pthread_mutex_lock(&handle->cmd_queue.lock);
32 /* Add to queue. */
33 cds_list_add_tail(&cmd->cmd_list_node,
34 &handle->cmd_queue.list);
35 /* Wake-up thread. */
58d3fed5 36 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
ab0ee2ca 37 &notification_counter, sizeof(notification_counter));
58d3fed5 38 if (ret != sizeof(notification_counter)) {
ab0ee2ca
JG
39 PERROR("write to notification thread's queue event fd");
40 /*
41 * Remove the command from the list so the notification
42 * thread does not process it.
43 */
44 cds_list_del(&cmd->cmd_list_node);
45 goto error_unlock_queue;
46 }
47 pthread_mutex_unlock(&handle->cmd_queue.lock);
48
8ada111f 49 lttng_waiter_wait(&cmd->reply_waiter);
ab0ee2ca
JG
50 return 0;
51error_unlock_queue:
52 pthread_mutex_unlock(&handle->cmd_queue.lock);
53 return -1;
54}
55
1831ae68
FD
56static
57struct notification_thread_command *notification_thread_command_copy(
58 const struct notification_thread_command *original_cmd)
59{
60 struct notification_thread_command *new_cmd;
61
62 new_cmd = zmalloc(sizeof(*new_cmd));
63 if (!new_cmd) {
64 goto end;
65 }
66
67 *new_cmd = *original_cmd;
68 init_notification_thread_command(new_cmd);
69end:
70 return new_cmd;
71}
72
73static
74int run_command_no_wait(struct notification_thread_handle *handle,
75 const struct notification_thread_command *in_cmd)
76{
77 int ret;
78 uint64_t notification_counter = 1;
79 struct notification_thread_command *new_cmd =
80 notification_thread_command_copy(in_cmd);
81
82 if (!new_cmd) {
83 goto error;
84 }
85 new_cmd->is_async = true;
86
87 pthread_mutex_lock(&handle->cmd_queue.lock);
88 /* Add to queue. */
89 cds_list_add_tail(&new_cmd->cmd_list_node,
90 &handle->cmd_queue.list);
91 /* Wake-up thread. */
92 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
93 &notification_counter, sizeof(notification_counter));
94 if (ret != sizeof(notification_counter)) {
95 PERROR("write to notification thread's queue event fd");
96 /*
97 * Remove the command from the list so the notification
98 * thread does not process it.
99 */
100 cds_list_del(&new_cmd->cmd_list_node);
101 goto error_unlock_queue;
102 }
103 pthread_mutex_unlock(&handle->cmd_queue.lock);
104 return 0;
105error_unlock_queue:
106 free(new_cmd);
107 pthread_mutex_unlock(&handle->cmd_queue.lock);
108error:
109 return -1;
110}
111
ab0ee2ca
JG
112enum lttng_error_code notification_thread_command_register_trigger(
113 struct notification_thread_handle *handle,
114 struct lttng_trigger *trigger)
115{
116 int ret;
117 enum lttng_error_code ret_code;
1831ae68 118 struct notification_thread_command cmd = {};
ab0ee2ca
JG
119
120 init_notification_thread_command(&cmd);
121
122 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
123 cmd.parameters.trigger = trigger;
124
125 ret = run_command_wait(handle, &cmd);
126 if (ret) {
127 ret_code = LTTNG_ERR_UNK;
128 goto end;
129 }
130 ret_code = cmd.reply_code;
131end:
132 return ret_code;
133}
134
135enum lttng_error_code notification_thread_command_unregister_trigger(
136 struct notification_thread_handle *handle,
137 struct lttng_trigger *trigger)
138{
139 int ret;
140 enum lttng_error_code ret_code;
1831ae68 141 struct notification_thread_command cmd = {};
ab0ee2ca
JG
142
143 init_notification_thread_command(&cmd);
144
145 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
146 cmd.parameters.trigger = trigger;
147
148 ret = run_command_wait(handle, &cmd);
149 if (ret) {
150 ret_code = LTTNG_ERR_UNK;
151 goto end;
152 }
153 ret_code = cmd.reply_code;
154end:
155 return ret_code;
156}
157
158enum lttng_error_code notification_thread_command_add_channel(
159 struct notification_thread_handle *handle,
160 char *session_name, uid_t uid, gid_t gid,
161 char *channel_name, uint64_t key,
162 enum lttng_domain_type domain, uint64_t capacity)
163{
164 int ret;
165 enum lttng_error_code ret_code;
1831ae68 166 struct notification_thread_command cmd = {};
ab0ee2ca
JG
167
168 init_notification_thread_command(&cmd);
169
170 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
8abe313a
JG
171 cmd.parameters.add_channel.session.name = session_name;
172 cmd.parameters.add_channel.session.uid = uid;
173 cmd.parameters.add_channel.session.gid = gid;
174 cmd.parameters.add_channel.channel.name = channel_name;
175 cmd.parameters.add_channel.channel.key = key;
176 cmd.parameters.add_channel.channel.domain = domain;
177 cmd.parameters.add_channel.channel.capacity = capacity;
ab0ee2ca
JG
178
179 ret = run_command_wait(handle, &cmd);
180 if (ret) {
181 ret_code = LTTNG_ERR_UNK;
182 goto end;
183 }
184 ret_code = cmd.reply_code;
185end:
186 return ret_code;
187}
188
189enum lttng_error_code notification_thread_command_remove_channel(
190 struct notification_thread_handle *handle,
191 uint64_t key, enum lttng_domain_type domain)
192{
193 int ret;
194 enum lttng_error_code ret_code;
1831ae68 195 struct notification_thread_command cmd = {};
ab0ee2ca
JG
196
197 init_notification_thread_command(&cmd);
198
199 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
200 cmd.parameters.remove_channel.key = key;
201 cmd.parameters.remove_channel.domain = domain;
202
203 ret = run_command_wait(handle, &cmd);
204 if (ret) {
205 ret_code = LTTNG_ERR_UNK;
206 goto end;
207 }
208 ret_code = cmd.reply_code;
209end:
210 return ret_code;
211}
212
731c1b12
JG
213enum lttng_error_code notification_thread_command_session_rotation_ongoing(
214 struct notification_thread_handle *handle,
215 const char *session_name, uid_t uid, gid_t gid,
216 uint64_t trace_archive_chunk_id)
217{
218 int ret;
219 enum lttng_error_code ret_code;
1831ae68 220 struct notification_thread_command cmd = {};
731c1b12
JG
221
222 init_notification_thread_command(&cmd);
223
224 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
225 cmd.parameters.session_rotation.session_name = session_name;
226 cmd.parameters.session_rotation.uid = uid;
227 cmd.parameters.session_rotation.gid = gid;
228 cmd.parameters.session_rotation.trace_archive_chunk_id =
229 trace_archive_chunk_id;
230
231 ret = run_command_wait(handle, &cmd);
232 if (ret) {
233 ret_code = LTTNG_ERR_UNK;
234 goto end;
235 }
236 ret_code = cmd.reply_code;
237end:
238 return ret_code;
239}
240
241enum lttng_error_code notification_thread_command_session_rotation_completed(
242 struct notification_thread_handle *handle,
243 const char *session_name, uid_t uid, gid_t gid,
244 uint64_t trace_archive_chunk_id,
245 struct lttng_trace_archive_location *location)
246{
247 int ret;
248 enum lttng_error_code ret_code;
1831ae68 249 struct notification_thread_command cmd = {};
731c1b12
JG
250
251 init_notification_thread_command(&cmd);
252
253 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED;
254 cmd.parameters.session_rotation.session_name = session_name;
255 cmd.parameters.session_rotation.uid = uid;
256 cmd.parameters.session_rotation.gid = gid;
257 cmd.parameters.session_rotation.trace_archive_chunk_id =
258 trace_archive_chunk_id;
259 cmd.parameters.session_rotation.location = location;
260
261 ret = run_command_wait(handle, &cmd);
262 if (ret) {
263 ret_code = LTTNG_ERR_UNK;
264 goto end;
265 }
266 ret_code = cmd.reply_code;
267end:
268 return ret_code;
269}
270
1831ae68
FD
271enum lttng_error_code notification_thread_command_add_application(
272 struct notification_thread_handle *handle,
273 struct lttng_pipe *pipe)
274{
275 int ret;
276 enum lttng_error_code ret_code;
277 struct notification_thread_command cmd = {};
278
279 init_notification_thread_command(&cmd);
280
281 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION;
282 cmd.parameters.application.read_side_trigger_event_application_pipe = lttng_pipe_get_readfd(pipe);
283
284 ret = run_command_wait(handle, &cmd);
285 if (ret) {
286 ret_code = LTTNG_ERR_UNK;
287 goto end;
288 }
289 ret_code = cmd.reply_code;
290end:
291 return ret_code;
292}
293
294enum lttng_error_code notification_thread_command_remove_application(
295 struct notification_thread_handle *handle,
296 struct lttng_pipe *pipe)
297{
298 int ret;
299 enum lttng_error_code ret_code;
300 struct notification_thread_command cmd = {};
301
302 init_notification_thread_command(&cmd);
303
304 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION;
305 cmd.parameters.application.read_side_trigger_event_application_pipe = lttng_pipe_get_readfd(pipe);
306
307 ret = run_command_wait(handle, &cmd);
308 if (ret) {
309 ret_code = LTTNG_ERR_UNK;
310 goto end;
311 }
312 ret_code = cmd.reply_code;
313end:
314 return ret_code;
315}
316
317enum lttng_error_code notification_thread_command_get_tokens(
318 struct notification_thread_handle *handle,
319 struct lttng_triggers **tokens_triggers)
320{
321 int ret;
322 enum lttng_error_code ret_code;
323 struct notification_thread_command cmd = {};
324
325 assert(handle);
326 assert(tokens_triggers);
327
328 init_notification_thread_command(&cmd);
329
330 cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TOKENS;
331
332 ret = run_command_wait(handle, &cmd);
333 if (ret) {
334 ret_code = LTTNG_ERR_UNK;
335 goto end;
336 }
337 ret_code = cmd.reply_code;
338 *tokens_triggers = cmd.reply.get_tokens.triggers;
339
340end:
341 return ret_code;
342}
343
344enum lttng_error_code notification_thread_command_list_triggers(
345 struct notification_thread_handle *handle,
346 uid_t uid,
347 gid_t gid,
348 struct lttng_triggers **triggers)
349{
350 int ret;
351 enum lttng_error_code ret_code;
352 struct notification_thread_command cmd = {};
353
354 assert(handle);
355 assert(triggers);
356
357 init_notification_thread_command(&cmd);
358
359 cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS;
360 cmd.parameters.list_triggers.uid = uid;
361 cmd.parameters.list_triggers.gid = gid;
362
363 ret = run_command_wait(handle, &cmd);
364 if (ret) {
365 ret_code = LTTNG_ERR_UNK;
366 goto end;
367 }
368 ret_code = cmd.reply_code;
369 *triggers = cmd.reply.list_triggers.triggers;
370
371end:
372 return ret_code;
373}
374
ab0ee2ca
JG
375void notification_thread_command_quit(
376 struct notification_thread_handle *handle)
377{
378 int ret;
1831ae68 379 struct notification_thread_command cmd = {};
ab0ee2ca
JG
380
381 init_notification_thread_command(&cmd);
382
383 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
384 ret = run_command_wait(handle, &cmd);
385 assert(!ret && cmd.reply_code == LTTNG_OK);
386}
This page took 0.056595 seconds and 5 git commands to generate.