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();;
54 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
58 static struct notification_thread_command
*
59 notification_thread_command_copy(const struct notification_thread_command
*original_cmd
)
61 struct notification_thread_command
*new_cmd
;
64 new_cmd
= new notification_thread_command
;
65 } catch (const std::bad_alloc
&e
) {
66 ERR("Failed to allocate notification_thread_command: %s", e
.what());
70 *new_cmd
= *original_cmd
;
71 init_notification_thread_command(new_cmd
);
75 static int run_command_no_wait(struct notification_thread_handle
*handle
,
76 const struct notification_thread_command
*in_cmd
)
79 uint64_t notification_counter
= 1;
80 struct notification_thread_command
*new_cmd
= notification_thread_command_copy(in_cmd
);
85 new_cmd
->is_async
= true;
87 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
89 cds_list_add_tail(&new_cmd
->cmd_list_node
, &handle
->cmd_queue
.list
);
92 handle
->cmd_queue
.event_fd
, ¬ification_counter
, sizeof(notification_counter
));
93 if (ret
!= sizeof(notification_counter
)) {
94 PERROR("write to notification thread's queue event fd");
96 * Remove the command from the list so the notification
97 * thread does not process it.
99 cds_list_del(&new_cmd
->cmd_list_node
);
100 goto error_unlock_queue
;
103 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
108 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
113 enum lttng_error_code
114 notification_thread_command_register_trigger(struct notification_thread_handle
*handle
,
115 struct lttng_trigger
*trigger
,
116 bool is_trigger_anonymous
)
119 enum lttng_error_code ret_code
;
120 notification_thread_command cmd
;
122 LTTNG_ASSERT(trigger
);
123 init_notification_thread_command(&cmd
);
125 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
126 lttng_trigger_get(trigger
);
127 cmd
.parameters
.register_trigger
.trigger
= trigger
;
128 cmd
.parameters
.register_trigger
.is_trigger_anonymous
= is_trigger_anonymous
;
130 ret
= run_command_wait(handle
, &cmd
);
132 ret_code
= LTTNG_ERR_UNK
;
135 ret_code
= cmd
.reply_code
;
140 enum lttng_error_code
141 notification_thread_command_unregister_trigger(struct notification_thread_handle
*handle
,
142 const struct lttng_trigger
*trigger
)
145 enum lttng_error_code ret_code
;
146 notification_thread_command cmd
;
148 init_notification_thread_command(&cmd
);
150 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
151 cmd
.parameters
.unregister_trigger
.trigger
= trigger
;
153 ret
= run_command_wait(handle
, &cmd
);
155 ret_code
= LTTNG_ERR_UNK
;
158 ret_code
= cmd
.reply_code
;
163 enum lttng_error_code
164 notification_thread_command_add_session(struct notification_thread_handle
*handle
,
166 const char *session_name
,
171 enum lttng_error_code ret_code
;
172 notification_thread_command cmd
;
174 init_notification_thread_command(&cmd
);
176 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_SESSION
;
177 cmd
.parameters
.add_session
.session_id
= session_id
;
178 cmd
.parameters
.add_session
.session_name
= session_name
;
179 cmd
.parameters
.add_session
.session_uid
= session_uid
;
180 cmd
.parameters
.add_session
.session_gid
= session_gid
;
182 ret
= run_command_wait(handle
, &cmd
);
184 ret_code
= LTTNG_ERR_UNK
;
187 ret_code
= cmd
.reply_code
;
192 enum lttng_error_code
193 notification_thread_command_remove_session(struct notification_thread_handle
*handle
,
197 enum lttng_error_code ret_code
;
198 notification_thread_command cmd
;
200 init_notification_thread_command(&cmd
);
202 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
;
203 cmd
.parameters
.remove_session
.session_id
= session_id
;
205 ret
= run_command_wait(handle
, &cmd
);
207 ret_code
= LTTNG_ERR_UNK
;
210 ret_code
= cmd
.reply_code
;
215 enum lttng_error_code
216 notification_thread_command_add_channel(struct notification_thread_handle
*handle
,
220 enum lttng_domain_type domain
,
224 enum lttng_error_code ret_code
;
225 notification_thread_command cmd
;
227 init_notification_thread_command(&cmd
);
229 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
230 cmd
.parameters
.add_channel
.session
.id
= session_id
;
231 cmd
.parameters
.add_channel
.channel
.name
= channel_name
;
232 cmd
.parameters
.add_channel
.channel
.key
= key
;
233 cmd
.parameters
.add_channel
.channel
.domain
= domain
;
234 cmd
.parameters
.add_channel
.channel
.capacity
= capacity
;
236 ret
= run_command_wait(handle
, &cmd
);
238 ret_code
= LTTNG_ERR_UNK
;
241 ret_code
= cmd
.reply_code
;
246 enum lttng_error_code
notification_thread_command_remove_channel(
247 struct notification_thread_handle
*handle
, uint64_t key
, enum lttng_domain_type domain
)
250 enum lttng_error_code ret_code
;
251 notification_thread_command cmd
;
253 init_notification_thread_command(&cmd
);
255 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
256 cmd
.parameters
.remove_channel
.key
= key
;
257 cmd
.parameters
.remove_channel
.domain
= domain
;
259 ret
= run_command_wait(handle
, &cmd
);
261 ret_code
= LTTNG_ERR_UNK
;
264 ret_code
= cmd
.reply_code
;
269 enum lttng_error_code
270 notification_thread_command_session_rotation_ongoing(struct notification_thread_handle
*handle
,
272 uint64_t trace_archive_chunk_id
)
275 enum lttng_error_code ret_code
;
276 notification_thread_command cmd
;
278 init_notification_thread_command(&cmd
);
280 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
281 cmd
.parameters
.session_rotation
.session_id
= session_id
;
282 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
= trace_archive_chunk_id
;
284 ret
= run_command_wait(handle
, &cmd
);
286 ret_code
= LTTNG_ERR_UNK
;
289 ret_code
= cmd
.reply_code
;
294 enum lttng_error_code
notification_thread_command_session_rotation_completed(
295 struct notification_thread_handle
*handle
,
297 uint64_t trace_archive_chunk_id
,
298 struct lttng_trace_archive_location
*location
)
301 enum lttng_error_code ret_code
;
302 notification_thread_command cmd
;
304 init_notification_thread_command(&cmd
);
306 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
;
307 cmd
.parameters
.session_rotation
.session_id
= session_id
;
308 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
= trace_archive_chunk_id
;
309 cmd
.parameters
.session_rotation
.location
= location
;
311 ret
= run_command_wait(handle
, &cmd
);
313 ret_code
= LTTNG_ERR_UNK
;
316 ret_code
= cmd
.reply_code
;
321 enum lttng_error_code
322 notification_thread_command_add_tracer_event_source(struct notification_thread_handle
*handle
,
323 int tracer_event_source_fd
,
324 enum lttng_domain_type domain
)
327 enum lttng_error_code ret_code
;
328 notification_thread_command cmd
;
330 LTTNG_ASSERT(tracer_event_source_fd
>= 0);
332 init_notification_thread_command(&cmd
);
334 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
;
335 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
= tracer_event_source_fd
;
336 cmd
.parameters
.tracer_event_source
.domain
= domain
;
338 ret
= run_command_wait(handle
, &cmd
);
340 ret_code
= LTTNG_ERR_UNK
;
344 ret_code
= cmd
.reply_code
;
349 enum lttng_error_code
350 notification_thread_command_remove_tracer_event_source(struct notification_thread_handle
*handle
,
351 int tracer_event_source_fd
)
354 enum lttng_error_code ret_code
;
355 notification_thread_command cmd
;
357 init_notification_thread_command(&cmd
);
359 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
;
360 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
= tracer_event_source_fd
;
362 ret
= run_command_wait(handle
, &cmd
);
364 ret_code
= LTTNG_ERR_UNK
;
368 ret_code
= cmd
.reply_code
;
373 enum lttng_error_code
notification_thread_command_list_triggers(
374 struct notification_thread_handle
*handle
, uid_t uid
, struct lttng_triggers
**triggers
)
377 enum lttng_error_code ret_code
;
378 notification_thread_command cmd
;
380 LTTNG_ASSERT(handle
);
381 LTTNG_ASSERT(triggers
);
383 init_notification_thread_command(&cmd
);
385 cmd
.type
= NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
;
386 cmd
.parameters
.list_triggers
.uid
= uid
;
388 ret
= run_command_wait(handle
, &cmd
);
390 ret_code
= LTTNG_ERR_UNK
;
394 ret_code
= cmd
.reply_code
;
395 *triggers
= cmd
.reply
.list_triggers
.triggers
;
401 void notification_thread_command_quit(struct notification_thread_handle
*handle
)
404 notification_thread_command cmd
;
406 init_notification_thread_command(&cmd
);
408 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
409 ret
= run_command_wait(handle
, &cmd
);
410 LTTNG_ASSERT(!ret
&& cmd
.reply_code
== LTTNG_OK
);
413 int notification_thread_client_communication_update(
414 struct notification_thread_handle
*handle
,
415 notification_client_id id
,
416 enum client_transmission_status transmission_status
)
418 notification_thread_command cmd
;
420 init_notification_thread_command(&cmd
);
422 cmd
.type
= NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
;
423 cmd
.parameters
.client_communication_update
.id
= id
;
424 cmd
.parameters
.client_communication_update
.status
= transmission_status
;
425 return run_command_no_wait(handle
, &cmd
);
428 enum lttng_error_code
429 notification_thread_command_get_trigger(struct notification_thread_handle
*handle
,
430 const struct lttng_trigger
*trigger
,
431 struct lttng_trigger
**real_trigger
)
434 enum lttng_error_code ret_code
;
435 notification_thread_command cmd
;
437 init_notification_thread_command(&cmd
);
439 cmd
.type
= NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
;
440 cmd
.parameters
.get_trigger
.trigger
= trigger
;
441 ret
= run_command_wait(handle
, &cmd
);
443 ret_code
= LTTNG_ERR_UNK
;
447 ret_code
= cmd
.reply_code
;
448 *real_trigger
= cmd
.reply
.get_trigger
.trigger
;
455 * Takes ownership of the payload if present.
457 struct lttng_event_notifier_notification
*lttng_event_notifier_notification_create(
458 uint64_t tracer_token
, enum lttng_domain_type domain
, char *payload
, size_t payload_size
)
460 struct lttng_event_notifier_notification
*notification
= nullptr;
462 LTTNG_ASSERT(domain
!= LTTNG_DOMAIN_NONE
);
463 LTTNG_ASSERT((payload
&& payload_size
) || (!payload
&& !payload_size
));
465 notification
= zmalloc
<lttng_event_notifier_notification
>();
466 if (notification
== nullptr) {
467 ERR("Error allocating notification");
471 notification
->tracer_token
= tracer_token
;
472 notification
->type
= domain
;
473 notification
->capture_buffer
= payload
;
474 notification
->capture_buf_size
= payload_size
;
480 void lttng_event_notifier_notification_destroy(
481 struct lttng_event_notifier_notification
*notification
)
487 free(notification
->capture_buffer
);