2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #include <lttng/trigger/trigger.h>
19 #include <lttng/lttng-error.h>
20 #include "notification-thread.h"
21 #include "notification-thread-commands.h"
22 #include <common/error.h>
28 void init_notification_thread_command(struct notification_thread_command
*cmd
)
30 memset(cmd
, 0, sizeof(*cmd
));
31 CDS_INIT_LIST_HEAD(&cmd
->cmd_list_node
);
32 lttng_waiter_init(&cmd
->reply_waiter
);
36 int run_command_wait(struct notification_thread_handle
*handle
,
37 struct notification_thread_command
*cmd
)
40 uint64_t notification_counter
= 1;
42 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
44 cds_list_add_tail(&cmd
->cmd_list_node
,
45 &handle
->cmd_queue
.list
);
47 ret
= lttng_write(lttng_pipe_get_writefd(handle
->cmd_queue
.event_pipe
),
48 ¬ification_counter
, sizeof(notification_counter
));
49 if (ret
!= sizeof(notification_counter
)) {
50 PERROR("write to notification thread's queue event fd");
52 * Remove the command from the list so the notification
53 * thread does not process it.
55 cds_list_del(&cmd
->cmd_list_node
);
56 goto error_unlock_queue
;
58 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
60 lttng_waiter_wait(&cmd
->reply_waiter
);
63 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
67 enum lttng_error_code
notification_thread_command_register_trigger(
68 struct notification_thread_handle
*handle
,
69 struct lttng_trigger
*trigger
)
72 enum lttng_error_code ret_code
;
73 struct notification_thread_command cmd
;
75 init_notification_thread_command(&cmd
);
77 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
;
78 cmd
.parameters
.trigger
= trigger
;
80 ret
= run_command_wait(handle
, &cmd
);
82 ret_code
= LTTNG_ERR_UNK
;
85 ret_code
= cmd
.reply_code
;
90 enum lttng_error_code
notification_thread_command_unregister_trigger(
91 struct notification_thread_handle
*handle
,
92 struct lttng_trigger
*trigger
)
95 enum lttng_error_code ret_code
;
96 struct notification_thread_command cmd
;
98 init_notification_thread_command(&cmd
);
100 cmd
.type
= NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
;
101 cmd
.parameters
.trigger
= trigger
;
103 ret
= run_command_wait(handle
, &cmd
);
105 ret_code
= LTTNG_ERR_UNK
;
108 ret_code
= cmd
.reply_code
;
113 enum lttng_error_code
notification_thread_command_add_channel(
114 struct notification_thread_handle
*handle
,
115 char *session_name
, uid_t uid
, gid_t gid
,
116 char *channel_name
, uint64_t key
,
117 enum lttng_domain_type domain
, uint64_t capacity
)
120 enum lttng_error_code ret_code
;
121 struct notification_thread_command cmd
;
123 init_notification_thread_command(&cmd
);
125 cmd
.type
= NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
;
126 cmd
.parameters
.add_channel
.session
.name
= session_name
;
127 cmd
.parameters
.add_channel
.session
.uid
= uid
;
128 cmd
.parameters
.add_channel
.session
.gid
= gid
;
129 cmd
.parameters
.add_channel
.channel
.name
= channel_name
;
130 cmd
.parameters
.add_channel
.channel
.key
= key
;
131 cmd
.parameters
.add_channel
.channel
.domain
= domain
;
132 cmd
.parameters
.add_channel
.channel
.capacity
= capacity
;
134 ret
= run_command_wait(handle
, &cmd
);
136 ret_code
= LTTNG_ERR_UNK
;
139 ret_code
= cmd
.reply_code
;
144 enum lttng_error_code
notification_thread_command_remove_channel(
145 struct notification_thread_handle
*handle
,
146 uint64_t key
, enum lttng_domain_type domain
)
149 enum lttng_error_code ret_code
;
150 struct notification_thread_command cmd
;
152 init_notification_thread_command(&cmd
);
154 cmd
.type
= NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
;
155 cmd
.parameters
.remove_channel
.key
= key
;
156 cmd
.parameters
.remove_channel
.domain
= domain
;
158 ret
= run_command_wait(handle
, &cmd
);
160 ret_code
= LTTNG_ERR_UNK
;
163 ret_code
= cmd
.reply_code
;
168 enum lttng_error_code
notification_thread_command_session_rotation_ongoing(
169 struct notification_thread_handle
*handle
,
170 const char *session_name
, uid_t uid
, gid_t gid
,
171 uint64_t trace_archive_chunk_id
)
174 enum lttng_error_code ret_code
;
175 struct notification_thread_command cmd
;
177 init_notification_thread_command(&cmd
);
179 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
180 cmd
.parameters
.session_rotation
.session_name
= session_name
;
181 cmd
.parameters
.session_rotation
.uid
= uid
;
182 cmd
.parameters
.session_rotation
.gid
= gid
;
183 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
=
184 trace_archive_chunk_id
;
186 ret
= run_command_wait(handle
, &cmd
);
188 ret_code
= LTTNG_ERR_UNK
;
191 ret_code
= cmd
.reply_code
;
196 enum lttng_error_code
notification_thread_command_session_rotation_completed(
197 struct notification_thread_handle
*handle
,
198 const char *session_name
, uid_t uid
, gid_t gid
,
199 uint64_t trace_archive_chunk_id
,
200 struct lttng_trace_archive_location
*location
)
203 enum lttng_error_code ret_code
;
204 struct notification_thread_command cmd
;
206 init_notification_thread_command(&cmd
);
208 cmd
.type
= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
;
209 cmd
.parameters
.session_rotation
.session_name
= session_name
;
210 cmd
.parameters
.session_rotation
.uid
= uid
;
211 cmd
.parameters
.session_rotation
.gid
= gid
;
212 cmd
.parameters
.session_rotation
.trace_archive_chunk_id
=
213 trace_archive_chunk_id
;
214 cmd
.parameters
.session_rotation
.location
= location
;
216 ret
= run_command_wait(handle
, &cmd
);
218 ret_code
= LTTNG_ERR_UNK
;
221 ret_code
= cmd
.reply_code
;
226 void notification_thread_command_quit(
227 struct notification_thread_handle
*handle
)
230 struct notification_thread_command cmd
;
232 init_notification_thread_command(&cmd
);
234 cmd
.type
= NOTIFICATION_COMMAND_TYPE_QUIT
;
235 ret
= run_command_wait(handle
, &cmd
);
236 assert(!ret
&& cmd
.reply_code
== LTTNG_OK
);