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.h"
11 #include "notification-thread-commands.h"
12 #include <common/error.h>
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(sizeof(*new_cmd
));
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
)
117 enum lttng_error_code ret_code
;
118 struct notification_thread_command cmd
= {};
121 init_notification_thread_command(&cmd
);
123 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
124 lttng_trigger_get(trigger
);
125 cmd
.parameters
.register_trigger
.trigger
= trigger
;
127 ret
= run_command_wait(handle
, &cmd
);
129 ret_code
= LTTNG_ERR_UNK
;
132 ret_code
= cmd
.reply_code
;
137 enum lttng_error_code
notification_thread_command_unregister_trigger(
138 struct notification_thread_handle
*handle
,
139 const struct lttng_trigger
*trigger
)
142 enum lttng_error_code ret_code
;
143 struct notification_thread_command cmd
= {};
145 init_notification_thread_command(&cmd
);
147 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
148 cmd
.parameters
.unregister_trigger
.trigger
= trigger
;
150 ret
= run_command_wait(handle
, &cmd
);
152 ret_code
= LTTNG_ERR_UNK
;
155 ret_code
= cmd
.reply_code
;
160 enum 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
)
167 enum lttng_error_code ret_code
;
168 struct notification_thread_command cmd
= {};
170 init_notification_thread_command(&cmd
);
172 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
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
;
181 ret
= run_command_wait(handle
, &cmd
);
183 ret_code
= LTTNG_ERR_UNK
;
186 ret_code
= cmd
.reply_code
;
191 enum lttng_error_code
notification_thread_command_remove_channel(
192 struct notification_thread_handle
*handle
,
193 uint64_t key
, enum lttng_domain_type domain
)
196 enum lttng_error_code ret_code
;
197 struct notification_thread_command cmd
= {};
199 init_notification_thread_command(&cmd
);
201 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
202 cmd
.parameters
.remove_channel
.key
= key
;
203 cmd
.parameters
.remove_channel
.domain
= domain
;
205 ret
= run_command_wait(handle
, &cmd
);
207 ret_code
= LTTNG_ERR_UNK
;
210 ret_code
= cmd
.reply_code
;
215 enum 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
)
221 enum lttng_error_code ret_code
;
222 struct notification_thread_command cmd
= {};
224 init_notification_thread_command(&cmd
);
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
;
233 ret
= run_command_wait(handle
, &cmd
);
235 ret_code
= LTTNG_ERR_UNK
;
238 ret_code
= cmd
.reply_code
;
243 enum 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
)
250 enum lttng_error_code ret_code
;
251 struct notification_thread_command cmd
= {};
253 init_notification_thread_command(&cmd
);
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
;
263 ret
= run_command_wait(handle
, &cmd
);
265 ret_code
= LTTNG_ERR_UNK
;
268 ret_code
= cmd
.reply_code
;
273 enum 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
)
279 enum lttng_error_code ret_code
;
280 struct notification_thread_command cmd
= {};
282 assert(tracer_event_source_fd
>= 0);
284 init_notification_thread_command(&cmd
);
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
;
291 ret
= run_command_wait(handle
, &cmd
);
293 ret_code
= LTTNG_ERR_UNK
;
297 ret_code
= cmd
.reply_code
;
302 enum lttng_error_code
notification_thread_command_remove_tracer_event_source(
303 struct notification_thread_handle
*handle
,
304 int tracer_event_source_fd
)
307 enum lttng_error_code ret_code
;
308 struct notification_thread_command cmd
= {};
310 init_notification_thread_command(&cmd
);
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
;
316 ret
= run_command_wait(handle
, &cmd
);
318 ret_code
= LTTNG_ERR_UNK
;
322 ret_code
= cmd
.reply_code
;
327 enum lttng_error_code
notification_thread_command_list_triggers(
328 struct notification_thread_handle
*handle
,
330 struct lttng_triggers
**triggers
)
333 enum lttng_error_code ret_code
;
334 struct notification_thread_command cmd
= {};
339 init_notification_thread_command(&cmd
);
341 cmd
.type
= NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
;
342 cmd
.parameters
.list_triggers
.uid
= uid
;
344 ret
= run_command_wait(handle
, &cmd
);
346 ret_code
= LTTNG_ERR_UNK
;
350 ret_code
= cmd
.reply_code
;
351 *triggers
= cmd
.reply
.list_triggers
.triggers
;
357 void notification_thread_command_quit(
358 struct notification_thread_handle
*handle
)
361 struct notification_thread_command cmd
= {};
363 init_notification_thread_command(&cmd
);
365 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
366 ret
= run_command_wait(handle
, &cmd
);
367 assert(!ret
&& cmd
.reply_code
== LTTNG_OK
);
370 int notification_thread_client_communication_update(
371 struct notification_thread_handle
*handle
,
372 notification_client_id id
,
373 enum client_transmission_status transmission_status
)
375 struct notification_thread_command cmd
= {};
377 init_notification_thread_command(&cmd
);
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
);