2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "notification-thread-commands.hpp"
9 #include "notification-thread.hpp"
11 #include <common/error.hpp>
13 #include <lttng/lttng-error.h>
14 #include <lttng/trigger/trigger.h>
20 static void init_notification_thread_command(struct notification_thread_command
*cmd
)
22 CDS_INIT_LIST_HEAD(&cmd
->cmd_list_node
);
25 static int run_command_wait(struct notification_thread_handle
*handle
,
26 struct notification_thread_command
*cmd
)
29 uint64_t notification_counter
= 1;
31 lttng::synchro::waiter command_completion_waiter
;
32 cmd
->command_completed_waker
.emplace(command_completion_waiter
.get_waker());
34 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
36 cds_list_add_tail(&cmd
->cmd_list_node
, &handle
->cmd_queue
.list
);
39 handle
->cmd_queue
.event_fd
, ¬ification_counter
, sizeof(notification_counter
));
40 if (ret
!= sizeof(notification_counter
)) {
41 PERROR("write to notification thread's queue event fd");
43 * Remove the command from the list so the notification
44 * thread does not process it.
46 cds_list_del(&cmd
->cmd_list_node
);
47 goto error_unlock_queue
;
49 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
51 command_completion_waiter
.wait();
55 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
59 static struct notification_thread_command
*
60 notification_thread_command_copy(const struct notification_thread_command
*original_cmd
)
62 struct notification_thread_command
*new_cmd
;
65 new_cmd
= new notification_thread_command
;
66 } catch (const std::bad_alloc
& e
) {
67 ERR("Failed to allocate notification_thread_command: %s", e
.what());
71 *new_cmd
= *original_cmd
;
72 init_notification_thread_command(new_cmd
);
76 static int run_command_no_wait(struct notification_thread_handle
*handle
,
77 const struct notification_thread_command
*in_cmd
)
80 uint64_t notification_counter
= 1;
81 struct notification_thread_command
*new_cmd
= notification_thread_command_copy(in_cmd
);
86 new_cmd
->is_async
= true;
88 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
90 cds_list_add_tail(&new_cmd
->cmd_list_node
, &handle
->cmd_queue
.list
);
93 handle
->cmd_queue
.event_fd
, ¬ification_counter
, sizeof(notification_counter
));
94 if (ret
!= sizeof(notification_counter
)) {
95 PERROR("write to notification thread's queue event fd");
97 * Remove the command from the list so the notification
98 * thread does not process it.
100 cds_list_del(&new_cmd
->cmd_list_node
);
101 goto error_unlock_queue
;
104 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
109 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
114 enum lttng_error_code
115 notification_thread_command_register_trigger(struct notification_thread_handle
*handle
,
116 struct lttng_trigger
*trigger
,
117 bool is_trigger_anonymous
)
120 enum lttng_error_code ret_code
;
121 notification_thread_command cmd
;
123 LTTNG_ASSERT(trigger
);
124 init_notification_thread_command(&cmd
);
126 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
127 lttng_trigger_get(trigger
);
128 cmd
.parameters
.register_trigger
.trigger
= trigger
;
129 cmd
.parameters
.register_trigger
.is_trigger_anonymous
= is_trigger_anonymous
;
131 ret
= run_command_wait(handle
, &cmd
);
133 ret_code
= LTTNG_ERR_UNK
;
136 ret_code
= cmd
.reply_code
;
141 enum lttng_error_code
142 notification_thread_command_unregister_trigger(struct notification_thread_handle
*handle
,
143 const struct lttng_trigger
*trigger
)
146 enum lttng_error_code ret_code
;
147 notification_thread_command cmd
;
149 init_notification_thread_command(&cmd
);
151 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
152 cmd
.parameters
.unregister_trigger
.trigger
= trigger
;
154 ret
= run_command_wait(handle
, &cmd
);
156 ret_code
= LTTNG_ERR_UNK
;
159 ret_code
= cmd
.reply_code
;
164 enum lttng_error_code
165 notification_thread_command_add_session(struct notification_thread_handle
*handle
,
167 const char *session_name
,
172 enum lttng_error_code ret_code
;
173 notification_thread_command cmd
;
175 init_notification_thread_command(&cmd
);
177 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_SESSION
;
178 cmd
.parameters
.add_session
.session_id
= session_id
;
179 cmd
.parameters
.add_session
.session_name
= session_name
;
180 cmd
.parameters
.add_session
.session_uid
= session_uid
;
181 cmd
.parameters
.add_session
.session_gid
= session_gid
;
183 ret
= run_command_wait(handle
, &cmd
);
185 ret_code
= LTTNG_ERR_UNK
;
188 ret_code
= cmd
.reply_code
;
193 enum lttng_error_code
194 notification_thread_command_remove_session(struct notification_thread_handle
*handle
,
198 enum lttng_error_code ret_code
;
199 notification_thread_command cmd
;
201 init_notification_thread_command(&cmd
);
203 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
;
204 cmd
.parameters
.remove_session
.session_id
= session_id
;
206 ret
= run_command_wait(handle
, &cmd
);
208 ret_code
= LTTNG_ERR_UNK
;
211 ret_code
= cmd
.reply_code
;
216 enum lttng_error_code
217 notification_thread_command_add_channel(struct notification_thread_handle
*handle
,
221 enum lttng_domain_type domain
,
225 enum lttng_error_code ret_code
;
226 notification_thread_command cmd
;
228 init_notification_thread_command(&cmd
);
230 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
231 cmd
.parameters
.add_channel
.session
.id
= session_id
;
232 cmd
.parameters
.add_channel
.channel
.name
= channel_name
;
233 cmd
.parameters
.add_channel
.channel
.key
= key
;
234 cmd
.parameters
.add_channel
.channel
.domain
= domain
;
235 cmd
.parameters
.add_channel
.channel
.capacity
= capacity
;
237 ret
= run_command_wait(handle
, &cmd
);
239 ret_code
= LTTNG_ERR_UNK
;
242 ret_code
= cmd
.reply_code
;
247 enum lttng_error_code
notification_thread_command_remove_channel(
248 struct notification_thread_handle
*handle
, uint64_t key
, enum lttng_domain_type domain
)
251 enum lttng_error_code ret_code
;
252 notification_thread_command cmd
;
254 init_notification_thread_command(&cmd
);
256 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
257 cmd
.parameters
.remove_channel
.key
= key
;
258 cmd
.parameters
.remove_channel
.domain
= domain
;
260 ret
= run_command_wait(handle
, &cmd
);
262 ret_code
= LTTNG_ERR_UNK
;
265 ret_code
= cmd
.reply_code
;
270 enum lttng_error_code
271 notification_thread_command_session_rotation_ongoing(struct notification_thread_handle
*handle
,
273 uint64_t trace_archive_chunk_id
)
276 enum lttng_error_code ret_code
;
277 notification_thread_command cmd
;
279 init_notification_thread_command(&cmd
);
281 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
282 cmd
.parameters
.session_rotation
.session_id
= session_id
;
283 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
= trace_archive_chunk_id
;
285 ret
= run_command_wait(handle
, &cmd
);
287 ret_code
= LTTNG_ERR_UNK
;
290 ret_code
= cmd
.reply_code
;
295 enum lttng_error_code
notification_thread_command_session_rotation_completed(
296 struct notification_thread_handle
*handle
,
298 uint64_t trace_archive_chunk_id
,
299 struct lttng_trace_archive_location
*location
)
302 enum lttng_error_code ret_code
;
303 notification_thread_command cmd
;
305 init_notification_thread_command(&cmd
);
307 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
;
308 cmd
.parameters
.session_rotation
.session_id
= session_id
;
309 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
= trace_archive_chunk_id
;
310 cmd
.parameters
.session_rotation
.location
= location
;
312 ret
= run_command_wait(handle
, &cmd
);
314 ret_code
= LTTNG_ERR_UNK
;
317 ret_code
= cmd
.reply_code
;
322 enum lttng_error_code
323 notification_thread_command_add_tracer_event_source(struct notification_thread_handle
*handle
,
324 int tracer_event_source_fd
,
325 enum lttng_domain_type domain
)
328 enum lttng_error_code ret_code
;
329 notification_thread_command cmd
;
331 LTTNG_ASSERT(tracer_event_source_fd
>= 0);
333 init_notification_thread_command(&cmd
);
335 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
;
336 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
= tracer_event_source_fd
;
337 cmd
.parameters
.tracer_event_source
.domain
= domain
;
339 ret
= run_command_wait(handle
, &cmd
);
341 ret_code
= LTTNG_ERR_UNK
;
345 ret_code
= cmd
.reply_code
;
350 enum lttng_error_code
351 notification_thread_command_remove_tracer_event_source(struct notification_thread_handle
*handle
,
352 int tracer_event_source_fd
)
355 enum lttng_error_code ret_code
;
356 notification_thread_command cmd
;
358 init_notification_thread_command(&cmd
);
360 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
;
361 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
= tracer_event_source_fd
;
363 ret
= run_command_wait(handle
, &cmd
);
365 ret_code
= LTTNG_ERR_UNK
;
369 ret_code
= cmd
.reply_code
;
374 enum lttng_error_code
notification_thread_command_list_triggers(
375 struct notification_thread_handle
*handle
, uid_t uid
, struct lttng_triggers
**triggers
)
378 enum lttng_error_code ret_code
;
379 notification_thread_command cmd
;
381 LTTNG_ASSERT(handle
);
382 LTTNG_ASSERT(triggers
);
384 init_notification_thread_command(&cmd
);
386 cmd
.type
= NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
;
387 cmd
.parameters
.list_triggers
.uid
= uid
;
389 ret
= run_command_wait(handle
, &cmd
);
391 ret_code
= LTTNG_ERR_UNK
;
395 ret_code
= cmd
.reply_code
;
396 *triggers
= cmd
.reply
.list_triggers
.triggers
;
402 void notification_thread_command_quit(struct notification_thread_handle
*handle
)
405 notification_thread_command cmd
;
407 init_notification_thread_command(&cmd
);
409 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
410 ret
= run_command_wait(handle
, &cmd
);
411 LTTNG_ASSERT(!ret
&& cmd
.reply_code
== LTTNG_OK
);
414 int notification_thread_client_communication_update(
415 struct notification_thread_handle
*handle
,
416 notification_client_id id
,
417 enum client_transmission_status transmission_status
)
419 notification_thread_command cmd
;
421 init_notification_thread_command(&cmd
);
423 cmd
.type
= NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
;
424 cmd
.parameters
.client_communication_update
.id
= id
;
425 cmd
.parameters
.client_communication_update
.status
= transmission_status
;
426 return run_command_no_wait(handle
, &cmd
);
429 enum lttng_error_code
430 notification_thread_command_get_trigger(struct notification_thread_handle
*handle
,
431 const struct lttng_trigger
*trigger
,
432 struct lttng_trigger
**real_trigger
)
435 enum lttng_error_code ret_code
;
436 notification_thread_command cmd
;
438 init_notification_thread_command(&cmd
);
440 cmd
.type
= NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
;
441 cmd
.parameters
.get_trigger
.trigger
= trigger
;
442 ret
= run_command_wait(handle
, &cmd
);
444 ret_code
= LTTNG_ERR_UNK
;
448 ret_code
= cmd
.reply_code
;
449 *real_trigger
= cmd
.reply
.get_trigger
.trigger
;
456 * Takes ownership of the payload if present.
458 struct lttng_event_notifier_notification
*lttng_event_notifier_notification_create(
459 uint64_t tracer_token
, enum lttng_domain_type domain
, char *payload
, size_t payload_size
)
461 struct lttng_event_notifier_notification
*notification
= nullptr;
463 LTTNG_ASSERT(domain
!= LTTNG_DOMAIN_NONE
);
464 LTTNG_ASSERT((payload
&& payload_size
) || (!payload
&& !payload_size
));
466 notification
= zmalloc
<lttng_event_notifier_notification
>();
467 if (notification
== nullptr) {
468 ERR("Error allocating notification");
472 notification
->tracer_token
= tracer_token
;
473 notification
->type
= domain
;
474 notification
->capture_buffer
= payload
;
475 notification
->capture_buf_size
= payload_size
;
481 void lttng_event_notifier_notification_destroy(
482 struct lttng_event_notifier_notification
*notification
)
488 free(notification
->capture_buffer
);