2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <lttng/trigger/trigger.h>
9 #include <lttng/lttng-error.h>
10 #include "notification-thread.hpp"
11 #include "notification-thread-commands.hpp"
12 #include <common/error.hpp>
18 void init_notification_thread_command(struct notification_thread_command
*cmd
)
20 CDS_INIT_LIST_HEAD(&cmd
->cmd_list_node
);
21 lttng_waiter_init(&cmd
->reply_waiter
);
25 int run_command_wait(struct notification_thread_handle
*handle
,
26 struct notification_thread_command
*cmd
)
29 uint64_t notification_counter
= 1;
31 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
33 cds_list_add_tail(&cmd
->cmd_list_node
,
34 &handle
->cmd_queue
.list
);
36 ret
= lttng_write(lttng_pipe_get_writefd(handle
->cmd_queue
.event_pipe
),
37 ¬ification_counter
, sizeof(notification_counter
));
38 if (ret
!= sizeof(notification_counter
)) {
39 PERROR("write to notification thread's queue event fd");
41 * Remove the command from the list so the notification
42 * thread does not process it.
44 cds_list_del(&cmd
->cmd_list_node
);
45 goto error_unlock_queue
;
47 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
49 lttng_waiter_wait(&cmd
->reply_waiter
);
52 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
57 struct notification_thread_command
*notification_thread_command_copy(
58 const struct notification_thread_command
*original_cmd
)
60 struct notification_thread_command
*new_cmd
;
62 new_cmd
= zmalloc
<notification_thread_command
>();
67 *new_cmd
= *original_cmd
;
68 init_notification_thread_command(new_cmd
);
74 int run_command_no_wait(struct notification_thread_handle
*handle
,
75 const struct notification_thread_command
*in_cmd
)
78 uint64_t notification_counter
= 1;
79 struct notification_thread_command
*new_cmd
=
80 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
,
90 &handle
->cmd_queue
.list
);
92 ret
= lttng_write(lttng_pipe_get_writefd(handle
->cmd_queue
.event_pipe
),
93 ¬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
;
103 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
107 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
112 enum lttng_error_code
notification_thread_command_register_trigger(
113 struct notification_thread_handle
*handle
,
114 struct lttng_trigger
*trigger
,
115 bool is_trigger_anonymous
)
118 enum lttng_error_code ret_code
;
119 struct notification_thread_command cmd
= {};
121 LTTNG_ASSERT(trigger
);
122 init_notification_thread_command(&cmd
);
124 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
125 lttng_trigger_get(trigger
);
126 cmd
.parameters
.register_trigger
.trigger
= trigger
;
127 cmd
.parameters
.register_trigger
.is_trigger_anonymous
=
128 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
notification_thread_command_unregister_trigger(
141 struct notification_thread_handle
*handle
,
142 const struct lttng_trigger
*trigger
)
145 enum lttng_error_code ret_code
;
146 struct 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
notification_thread_command_add_channel(
164 struct notification_thread_handle
*handle
,
165 char *session_name
, uid_t uid
, gid_t gid
,
166 char *channel_name
, uint64_t key
,
167 enum lttng_domain_type domain
, uint64_t capacity
)
170 enum lttng_error_code ret_code
;
171 struct notification_thread_command cmd
= {};
173 init_notification_thread_command(&cmd
);
175 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
176 cmd
.parameters
.add_channel
.session
.name
= session_name
;
177 cmd
.parameters
.add_channel
.session
.uid
= uid
;
178 cmd
.parameters
.add_channel
.session
.gid
= gid
;
179 cmd
.parameters
.add_channel
.channel
.name
= channel_name
;
180 cmd
.parameters
.add_channel
.channel
.key
= key
;
181 cmd
.parameters
.add_channel
.channel
.domain
= domain
;
182 cmd
.parameters
.add_channel
.channel
.capacity
= capacity
;
184 ret
= run_command_wait(handle
, &cmd
);
186 ret_code
= LTTNG_ERR_UNK
;
189 ret_code
= cmd
.reply_code
;
194 enum lttng_error_code
notification_thread_command_remove_channel(
195 struct notification_thread_handle
*handle
,
196 uint64_t key
, enum lttng_domain_type domain
)
199 enum lttng_error_code ret_code
;
200 struct notification_thread_command cmd
= {};
202 init_notification_thread_command(&cmd
);
204 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
205 cmd
.parameters
.remove_channel
.key
= key
;
206 cmd
.parameters
.remove_channel
.domain
= domain
;
208 ret
= run_command_wait(handle
, &cmd
);
210 ret_code
= LTTNG_ERR_UNK
;
213 ret_code
= cmd
.reply_code
;
218 enum lttng_error_code
notification_thread_command_session_rotation_ongoing(
219 struct notification_thread_handle
*handle
,
220 const char *session_name
, uid_t uid
, gid_t gid
,
221 uint64_t trace_archive_chunk_id
)
224 enum lttng_error_code ret_code
;
225 struct notification_thread_command cmd
= {};
227 init_notification_thread_command(&cmd
);
229 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
230 cmd
.parameters
.session_rotation
.session_name
= session_name
;
231 cmd
.parameters
.session_rotation
.uid
= uid
;
232 cmd
.parameters
.session_rotation
.gid
= gid
;
233 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
=
234 trace_archive_chunk_id
;
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_session_rotation_completed(
247 struct notification_thread_handle
*handle
,
248 const char *session_name
, uid_t uid
, gid_t gid
,
249 uint64_t trace_archive_chunk_id
,
250 struct lttng_trace_archive_location
*location
)
253 enum lttng_error_code ret_code
;
254 struct notification_thread_command cmd
= {};
256 init_notification_thread_command(&cmd
);
258 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
;
259 cmd
.parameters
.session_rotation
.session_name
= session_name
;
260 cmd
.parameters
.session_rotation
.uid
= uid
;
261 cmd
.parameters
.session_rotation
.gid
= gid
;
262 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
=
263 trace_archive_chunk_id
;
264 cmd
.parameters
.session_rotation
.location
= location
;
266 ret
= run_command_wait(handle
, &cmd
);
268 ret_code
= LTTNG_ERR_UNK
;
271 ret_code
= cmd
.reply_code
;
276 enum lttng_error_code
notification_thread_command_add_tracer_event_source(
277 struct notification_thread_handle
*handle
,
278 int tracer_event_source_fd
,
279 enum lttng_domain_type domain
)
282 enum lttng_error_code ret_code
;
283 struct notification_thread_command cmd
= {};
285 LTTNG_ASSERT(tracer_event_source_fd
>= 0);
287 init_notification_thread_command(&cmd
);
289 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
;
290 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
=
291 tracer_event_source_fd
;
292 cmd
.parameters
.tracer_event_source
.domain
= domain
;
294 ret
= run_command_wait(handle
, &cmd
);
296 ret_code
= LTTNG_ERR_UNK
;
300 ret_code
= cmd
.reply_code
;
305 enum lttng_error_code
notification_thread_command_remove_tracer_event_source(
306 struct notification_thread_handle
*handle
,
307 int tracer_event_source_fd
)
310 enum lttng_error_code ret_code
;
311 struct notification_thread_command cmd
= {};
313 init_notification_thread_command(&cmd
);
315 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
;
316 cmd
.parameters
.tracer_event_source
.tracer_event_source_fd
=
317 tracer_event_source_fd
;
319 ret
= run_command_wait(handle
, &cmd
);
321 ret_code
= LTTNG_ERR_UNK
;
325 ret_code
= cmd
.reply_code
;
330 enum lttng_error_code
notification_thread_command_list_triggers(
331 struct notification_thread_handle
*handle
,
333 struct lttng_triggers
**triggers
)
336 enum lttng_error_code ret_code
;
337 struct notification_thread_command cmd
= {};
339 LTTNG_ASSERT(handle
);
340 LTTNG_ASSERT(triggers
);
342 init_notification_thread_command(&cmd
);
344 cmd
.type
= NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
;
345 cmd
.parameters
.list_triggers
.uid
= uid
;
347 ret
= run_command_wait(handle
, &cmd
);
349 ret_code
= LTTNG_ERR_UNK
;
353 ret_code
= cmd
.reply_code
;
354 *triggers
= cmd
.reply
.list_triggers
.triggers
;
360 void notification_thread_command_quit(
361 struct notification_thread_handle
*handle
)
364 struct notification_thread_command cmd
= {};
366 init_notification_thread_command(&cmd
);
368 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
369 ret
= run_command_wait(handle
, &cmd
);
370 LTTNG_ASSERT(!ret
&& cmd
.reply_code
== LTTNG_OK
);
373 int notification_thread_client_communication_update(
374 struct notification_thread_handle
*handle
,
375 notification_client_id id
,
376 enum client_transmission_status transmission_status
)
378 struct notification_thread_command cmd
= {};
380 init_notification_thread_command(&cmd
);
382 cmd
.type
= NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
;
383 cmd
.parameters
.client_communication_update
.id
= id
;
384 cmd
.parameters
.client_communication_update
.status
= transmission_status
;
385 return run_command_no_wait(handle
, &cmd
);
388 enum lttng_error_code
notification_thread_command_get_trigger(
389 struct notification_thread_handle
*handle
,
390 const struct lttng_trigger
*trigger
,
391 struct lttng_trigger
**real_trigger
)
394 enum lttng_error_code ret_code
;
395 struct notification_thread_command cmd
= {};
397 init_notification_thread_command(&cmd
);
399 cmd
.type
= NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
;
400 cmd
.parameters
.get_trigger
.trigger
= trigger
;
401 ret
= run_command_wait(handle
, &cmd
);
403 ret_code
= LTTNG_ERR_UNK
;
407 ret_code
= cmd
.reply_code
;
408 *real_trigger
= cmd
.reply
.get_trigger
.trigger
;
415 * Takes ownership of the payload if present.
417 struct lttng_event_notifier_notification
*lttng_event_notifier_notification_create(
418 uint64_t tracer_token
,
419 enum lttng_domain_type domain
,
423 struct lttng_event_notifier_notification
*notification
= NULL
;
425 LTTNG_ASSERT(domain
!= LTTNG_DOMAIN_NONE
);
426 LTTNG_ASSERT((payload
&& payload_size
) || (!payload
&& !payload_size
));
428 notification
= zmalloc
<lttng_event_notifier_notification
>();
429 if (notification
== NULL
) {
430 ERR("Error allocating notification");
434 notification
->tracer_token
= tracer_token
;
435 notification
->type
= domain
;
436 notification
->capture_buffer
= payload
;
437 notification
->capture_buf_size
= payload_size
;
443 void lttng_event_notifier_notification_destroy(
444 struct lttng_event_notifier_notification
*notification
)
450 free(notification
->capture_buffer
);