notification-thread: add `GET_TRIGGER` command
[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
0ab399e0
JG
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;
0ab399e0 118 struct notification_thread_command cmd = {};
ab0ee2ca 119
242388e4 120 assert(trigger);
ab0ee2ca
JG
121 init_notification_thread_command(&cmd);
122
123 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
242388e4 124 lttng_trigger_get(trigger);
ac16173e 125 cmd.parameters.register_trigger.trigger = trigger;
ab0ee2ca
JG
126
127 ret = run_command_wait(handle, &cmd);
128 if (ret) {
129 ret_code = LTTNG_ERR_UNK;
130 goto end;
131 }
132 ret_code = cmd.reply_code;
133end:
134 return ret_code;
135}
136
137enum lttng_error_code notification_thread_command_unregister_trigger(
138 struct notification_thread_handle *handle,
ac16173e 139 const struct lttng_trigger *trigger)
ab0ee2ca
JG
140{
141 int ret;
142 enum lttng_error_code ret_code;
0ab399e0 143 struct notification_thread_command cmd = {};
ab0ee2ca
JG
144
145 init_notification_thread_command(&cmd);
146
147 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
ac16173e 148 cmd.parameters.unregister_trigger.trigger = trigger;
ab0ee2ca
JG
149
150 ret = run_command_wait(handle, &cmd);
151 if (ret) {
152 ret_code = LTTNG_ERR_UNK;
153 goto end;
154 }
155 ret_code = cmd.reply_code;
156end:
157 return ret_code;
158}
159
160enum lttng_error_code notification_thread_command_add_channel(
161 struct notification_thread_handle *handle,
162 char *session_name, uid_t uid, gid_t gid,
163 char *channel_name, uint64_t key,
164 enum lttng_domain_type domain, uint64_t capacity)
165{
166 int ret;
167 enum lttng_error_code ret_code;
0ab399e0 168 struct notification_thread_command cmd = {};
ab0ee2ca
JG
169
170 init_notification_thread_command(&cmd);
171
172 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
8abe313a
JG
173 cmd.parameters.add_channel.session.name = session_name;
174 cmd.parameters.add_channel.session.uid = uid;
175 cmd.parameters.add_channel.session.gid = gid;
176 cmd.parameters.add_channel.channel.name = channel_name;
177 cmd.parameters.add_channel.channel.key = key;
178 cmd.parameters.add_channel.channel.domain = domain;
179 cmd.parameters.add_channel.channel.capacity = capacity;
ab0ee2ca
JG
180
181 ret = run_command_wait(handle, &cmd);
182 if (ret) {
183 ret_code = LTTNG_ERR_UNK;
184 goto end;
185 }
186 ret_code = cmd.reply_code;
187end:
188 return ret_code;
189}
190
191enum lttng_error_code notification_thread_command_remove_channel(
192 struct notification_thread_handle *handle,
193 uint64_t key, enum lttng_domain_type domain)
194{
195 int ret;
196 enum lttng_error_code ret_code;
0ab399e0 197 struct notification_thread_command cmd = {};
ab0ee2ca
JG
198
199 init_notification_thread_command(&cmd);
200
201 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
202 cmd.parameters.remove_channel.key = key;
203 cmd.parameters.remove_channel.domain = domain;
204
205 ret = run_command_wait(handle, &cmd);
206 if (ret) {
207 ret_code = LTTNG_ERR_UNK;
208 goto end;
209 }
210 ret_code = cmd.reply_code;
211end:
212 return ret_code;
213}
214
731c1b12
JG
215enum lttng_error_code notification_thread_command_session_rotation_ongoing(
216 struct notification_thread_handle *handle,
217 const char *session_name, uid_t uid, gid_t gid,
218 uint64_t trace_archive_chunk_id)
219{
220 int ret;
221 enum lttng_error_code ret_code;
0ab399e0 222 struct notification_thread_command cmd = {};
731c1b12
JG
223
224 init_notification_thread_command(&cmd);
225
226 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
227 cmd.parameters.session_rotation.session_name = session_name;
228 cmd.parameters.session_rotation.uid = uid;
229 cmd.parameters.session_rotation.gid = gid;
230 cmd.parameters.session_rotation.trace_archive_chunk_id =
231 trace_archive_chunk_id;
232
233 ret = run_command_wait(handle, &cmd);
234 if (ret) {
235 ret_code = LTTNG_ERR_UNK;
236 goto end;
237 }
238 ret_code = cmd.reply_code;
239end:
240 return ret_code;
241}
242
243enum lttng_error_code notification_thread_command_session_rotation_completed(
244 struct notification_thread_handle *handle,
245 const char *session_name, uid_t uid, gid_t gid,
246 uint64_t trace_archive_chunk_id,
247 struct lttng_trace_archive_location *location)
248{
249 int ret;
250 enum lttng_error_code ret_code;
0ab399e0 251 struct notification_thread_command cmd = {};
731c1b12
JG
252
253 init_notification_thread_command(&cmd);
254
255 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED;
256 cmd.parameters.session_rotation.session_name = session_name;
257 cmd.parameters.session_rotation.uid = uid;
258 cmd.parameters.session_rotation.gid = gid;
259 cmd.parameters.session_rotation.trace_archive_chunk_id =
260 trace_archive_chunk_id;
261 cmd.parameters.session_rotation.location = location;
262
263 ret = run_command_wait(handle, &cmd);
264 if (ret) {
265 ret_code = LTTNG_ERR_UNK;
266 goto end;
267 }
268 ret_code = cmd.reply_code;
269end:
270 return ret_code;
271}
272
d02d7404
JR
273enum lttng_error_code notification_thread_command_add_tracer_event_source(
274 struct notification_thread_handle *handle,
275 int tracer_event_source_fd,
276 enum lttng_domain_type domain)
277{
278 int ret;
279 enum lttng_error_code ret_code;
280 struct notification_thread_command cmd = {};
281
282 assert(tracer_event_source_fd >= 0);
283
284 init_notification_thread_command(&cmd);
285
286 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE;
287 cmd.parameters.tracer_event_source.tracer_event_source_fd =
288 tracer_event_source_fd;
289 cmd.parameters.tracer_event_source.domain = domain;
290
291 ret = run_command_wait(handle, &cmd);
292 if (ret) {
293 ret_code = LTTNG_ERR_UNK;
294 goto end;
295 }
296
297 ret_code = cmd.reply_code;
298end:
299 return ret_code;
300}
301
302enum lttng_error_code notification_thread_command_remove_tracer_event_source(
303 struct notification_thread_handle *handle,
304 int tracer_event_source_fd)
305{
306 int ret;
307 enum lttng_error_code ret_code;
308 struct notification_thread_command cmd = {};
309
310 init_notification_thread_command(&cmd);
311
312 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE;
313 cmd.parameters.tracer_event_source.tracer_event_source_fd =
314 tracer_event_source_fd;
315
316 ret = run_command_wait(handle, &cmd);
317 if (ret) {
318 ret_code = LTTNG_ERR_UNK;
319 goto end;
320 }
321
322 ret_code = cmd.reply_code;
323end:
324 return ret_code;
325}
326
fbc9f37d
JR
327enum lttng_error_code notification_thread_command_list_triggers(
328 struct notification_thread_handle *handle,
329 uid_t uid,
330 struct lttng_triggers **triggers)
331{
332 int ret;
333 enum lttng_error_code ret_code;
334 struct notification_thread_command cmd = {};
335
336 assert(handle);
337 assert(triggers);
338
339 init_notification_thread_command(&cmd);
340
341 cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS;
342 cmd.parameters.list_triggers.uid = uid;
343
344 ret = run_command_wait(handle, &cmd);
345 if (ret) {
346 ret_code = LTTNG_ERR_UNK;
347 goto end;
348 }
349
350 ret_code = cmd.reply_code;
351 *triggers = cmd.reply.list_triggers.triggers;
352
353end:
354 return ret_code;
355}
356
ab0ee2ca
JG
357void notification_thread_command_quit(
358 struct notification_thread_handle *handle)
359{
360 int ret;
0ab399e0 361 struct notification_thread_command cmd = {};
ab0ee2ca
JG
362
363 init_notification_thread_command(&cmd);
364
365 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
366 ret = run_command_wait(handle, &cmd);
367 assert(!ret && cmd.reply_code == LTTNG_OK);
368}
f2b3ef9f
JG
369
370int notification_thread_client_communication_update(
371 struct notification_thread_handle *handle,
372 notification_client_id id,
373 enum client_transmission_status transmission_status)
374{
375 struct notification_thread_command cmd = {};
376
377 init_notification_thread_command(&cmd);
378
379 cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE;
380 cmd.parameters.client_communication_update.id = id;
381 cmd.parameters.client_communication_update.status = transmission_status;
382 return run_command_no_wait(handle, &cmd);
383}
b9a8d78f 384
8790759c
FD
385enum lttng_error_code notification_thread_command_get_trigger(
386 struct notification_thread_handle *handle,
387 const struct lttng_trigger *trigger,
388 struct lttng_trigger **real_trigger)
389{
390 int ret;
391 enum lttng_error_code ret_code;
392 struct notification_thread_command cmd = {};
393
394 init_notification_thread_command(&cmd);
395
396 cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TRIGGER;
397 cmd.parameters.get_trigger.trigger = trigger;
398 ret = run_command_wait(handle, &cmd);
399 if (ret) {
400 ret_code = LTTNG_ERR_UNK;
401 goto end;
402 }
403
404 ret_code = cmd.reply_code;
405 *real_trigger = cmd.reply.get_trigger.trigger;
406
407end:
408 return ret_code;
409}
410
82b3cbf4
JR
411/*
412 * Takes ownership of the payload if present.
413 */
b9a8d78f 414LTTNG_HIDDEN
82b3cbf4
JR
415struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
416 uint64_t tracer_token,
417 enum lttng_domain_type domain,
418 char *payload,
419 size_t payload_size)
b9a8d78f
FD
420{
421 struct lttng_event_notifier_notification *notification = NULL;
422
423 assert(domain != LTTNG_DOMAIN_NONE);
82b3cbf4 424 assert((payload && payload_size) || (!payload && !payload_size));
b9a8d78f 425
82b3cbf4 426 notification = zmalloc(sizeof(struct lttng_event_notifier_notification));
b9a8d78f
FD
427 if (notification == NULL) {
428 ERR("[notification-thread] Error allocating notification");
429 goto end;
430 }
431
432 notification->tracer_token = tracer_token;
433 notification->type = domain;
82b3cbf4
JR
434 notification->capture_buffer = payload;
435 notification->capture_buf_size = payload_size;
b9a8d78f
FD
436
437end:
438 return notification;
439}
440
441LTTNG_HIDDEN
442void lttng_event_notifier_notification_destroy(
443 struct lttng_event_notifier_notification *notification)
444{
445 if (!notification) {
446 return;
447 }
448
82b3cbf4 449 free(notification->capture_buffer);
b9a8d78f
FD
450 free(notification);
451}
This page took 0.055256 seconds and 4 git commands to generate.