trigger: implement trigger naming
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-commands.c
CommitLineData
ab0ee2ca 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
ab0ee2ca 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
ab0ee2ca 5 *
ab0ee2ca
JG
6 */
7
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>
ab0ee2ca
JG
13#include <unistd.h>
14#include <stdint.h>
15#include <inttypes.h>
16
17static
18void init_notification_thread_command(struct notification_thread_command *cmd)
19{
ab0ee2ca 20 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
8ada111f 21 lttng_waiter_init(&cmd->reply_waiter);
ab0ee2ca
JG
22}
23
24static
25int run_command_wait(struct notification_thread_handle *handle,
26 struct notification_thread_command *cmd)
27{
28 int ret;
29 uint64_t notification_counter = 1;
30
ab0ee2ca
JG
31 pthread_mutex_lock(&handle->cmd_queue.lock);
32 /* Add to queue. */
33 cds_list_add_tail(&cmd->cmd_list_node,
34 &handle->cmd_queue.list);
35 /* Wake-up thread. */
58d3fed5 36 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
ab0ee2ca 37 &notification_counter, sizeof(notification_counter));
58d3fed5 38 if (ret != sizeof(notification_counter)) {
ab0ee2ca
JG
39 PERROR("write to notification thread's queue event fd");
40 /*
41 * Remove the command from the list so the notification
42 * thread does not process it.
43 */
44 cds_list_del(&cmd->cmd_list_node);
45 goto error_unlock_queue;
46 }
47 pthread_mutex_unlock(&handle->cmd_queue.lock);
48
8ada111f 49 lttng_waiter_wait(&cmd->reply_waiter);
ab0ee2ca
JG
50 return 0;
51error_unlock_queue:
52 pthread_mutex_unlock(&handle->cmd_queue.lock);
53 return -1;
54}
55
0ab399e0
JG
56static
57struct notification_thread_command *notification_thread_command_copy(
58 const struct notification_thread_command *original_cmd)
59{
60 struct notification_thread_command *new_cmd;
61
62 new_cmd = zmalloc(sizeof(*new_cmd));
63 if (!new_cmd) {
64 goto end;
65 }
66
67 *new_cmd = *original_cmd;
68 init_notification_thread_command(new_cmd);
69end:
70 return new_cmd;
71}
72
73static
74int run_command_no_wait(struct notification_thread_handle *handle,
75 const struct notification_thread_command *in_cmd)
76{
77 int ret;
78 uint64_t notification_counter = 1;
79 struct notification_thread_command *new_cmd =
80 notification_thread_command_copy(in_cmd);
81
82 if (!new_cmd) {
83 goto error;
84 }
85 new_cmd->is_async = true;
86
87 pthread_mutex_lock(&handle->cmd_queue.lock);
88 /* Add to queue. */
89 cds_list_add_tail(&new_cmd->cmd_list_node,
90 &handle->cmd_queue.list);
91 /* Wake-up thread. */
92 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
93 &notification_counter, sizeof(notification_counter));
94 if (ret != sizeof(notification_counter)) {
95 PERROR("write to notification thread's queue event fd");
96 /*
97 * Remove the command from the list so the notification
98 * thread does not process it.
99 */
100 cds_list_del(&new_cmd->cmd_list_node);
101 goto error_unlock_queue;
102 }
103 pthread_mutex_unlock(&handle->cmd_queue.lock);
104 return 0;
105error_unlock_queue:
106 free(new_cmd);
107 pthread_mutex_unlock(&handle->cmd_queue.lock);
108error:
109 return -1;
110}
111
ab0ee2ca
JG
112enum lttng_error_code notification_thread_command_register_trigger(
113 struct notification_thread_handle *handle,
114 struct lttng_trigger *trigger)
115{
116 int ret;
117 enum lttng_error_code ret_code;
0ab399e0 118 struct notification_thread_command cmd = {};
ab0ee2ca 119
242388e4 120 assert(trigger);
ab0ee2ca
JG
121 init_notification_thread_command(&cmd);
122
123 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
242388e4 124 lttng_trigger_get(trigger);
ab0ee2ca
JG
125 cmd.parameters.trigger = trigger;
126
127 ret = run_command_wait(handle, &cmd);
128 if (ret) {
129 ret_code = LTTNG_ERR_UNK;
130 goto end;
131 }
132 ret_code = cmd.reply_code;
133end:
134 return ret_code;
135}
136
137enum lttng_error_code notification_thread_command_unregister_trigger(
138 struct notification_thread_handle *handle,
139 struct lttng_trigger *trigger)
140{
141 int ret;
142 enum lttng_error_code ret_code;
0ab399e0 143 struct notification_thread_command cmd = {};
ab0ee2ca
JG
144
145 init_notification_thread_command(&cmd);
146
147 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
148 cmd.parameters.trigger = trigger;
149
150 ret = run_command_wait(handle, &cmd);
151 if (ret) {
152 ret_code = LTTNG_ERR_UNK;
153 goto end;
154 }
155 ret_code = cmd.reply_code;
156end:
157 return ret_code;
158}
159
160enum 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)
165{
166 int ret;
167 enum lttng_error_code ret_code;
0ab399e0 168 struct notification_thread_command cmd = {};
ab0ee2ca
JG
169
170 init_notification_thread_command(&cmd);
171
172 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
8abe313a
JG
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;
ab0ee2ca
JG
180
181 ret = run_command_wait(handle, &cmd);
182 if (ret) {
183 ret_code = LTTNG_ERR_UNK;
184 goto end;
185 }
186 ret_code = cmd.reply_code;
187end:
188 return ret_code;
189}
190
191enum lttng_error_code notification_thread_command_remove_channel(
192 struct notification_thread_handle *handle,
193 uint64_t key, enum lttng_domain_type domain)
194{
195 int ret;
196 enum lttng_error_code ret_code;
0ab399e0 197 struct notification_thread_command cmd = {};
ab0ee2ca
JG
198
199 init_notification_thread_command(&cmd);
200
201 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
202 cmd.parameters.remove_channel.key = key;
203 cmd.parameters.remove_channel.domain = domain;
204
205 ret = run_command_wait(handle, &cmd);
206 if (ret) {
207 ret_code = LTTNG_ERR_UNK;
208 goto end;
209 }
210 ret_code = cmd.reply_code;
211end:
212 return ret_code;
213}
214
731c1b12
JG
215enum 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)
219{
220 int ret;
221 enum lttng_error_code ret_code;
0ab399e0 222 struct notification_thread_command cmd = {};
731c1b12
JG
223
224 init_notification_thread_command(&cmd);
225
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;
232
233 ret = run_command_wait(handle, &cmd);
234 if (ret) {
235 ret_code = LTTNG_ERR_UNK;
236 goto end;
237 }
238 ret_code = cmd.reply_code;
239end:
240 return ret_code;
241}
242
243enum 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)
248{
249 int ret;
250 enum lttng_error_code ret_code;
0ab399e0 251 struct notification_thread_command cmd = {};
731c1b12
JG
252
253 init_notification_thread_command(&cmd);
254
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;
262
263 ret = run_command_wait(handle, &cmd);
264 if (ret) {
265 ret_code = LTTNG_ERR_UNK;
266 goto end;
267 }
268 ret_code = cmd.reply_code;
269end:
270 return ret_code;
271}
272
ab0ee2ca
JG
273void notification_thread_command_quit(
274 struct notification_thread_handle *handle)
275{
276 int ret;
0ab399e0 277 struct notification_thread_command cmd = {};
ab0ee2ca
JG
278
279 init_notification_thread_command(&cmd);
280
281 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
282 ret = run_command_wait(handle, &cmd);
283 assert(!ret && cmd.reply_code == LTTNG_OK);
284}
f2b3ef9f
JG
285
286int notification_thread_client_communication_update(
287 struct notification_thread_handle *handle,
288 notification_client_id id,
289 enum client_transmission_status transmission_status)
290{
291 struct notification_thread_command cmd = {};
292
293 init_notification_thread_command(&cmd);
294
295 cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE;
296 cmd.parameters.client_communication_update.id = id;
297 cmd.parameters.client_communication_update.status = transmission_status;
298 return run_command_no_wait(handle, &cmd);
299}
This page took 0.07211 seconds and 4 git commands to generate.