notification: fetch capture payload on notification reception
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-commands.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
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>
13 #include <unistd.h>
14 #include <stdint.h>
15 #include <inttypes.h>
16
17 static
18 void init_notification_thread_command(struct notification_thread_command *cmd)
19 {
20 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
21 lttng_waiter_init(&cmd->reply_waiter);
22 }
23
24 static
25 int 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
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. */
36 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
37 &notification_counter, sizeof(notification_counter));
38 if (ret != sizeof(notification_counter)) {
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
49 lttng_waiter_wait(&cmd->reply_waiter);
50 return 0;
51 error_unlock_queue:
52 pthread_mutex_unlock(&handle->cmd_queue.lock);
53 return -1;
54 }
55
56 static
57 struct 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);
69 end:
70 return new_cmd;
71 }
72
73 static
74 int 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;
105 error_unlock_queue:
106 free(new_cmd);
107 pthread_mutex_unlock(&handle->cmd_queue.lock);
108 error:
109 return -1;
110 }
111
112 enum 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;
118 struct notification_thread_command cmd = {};
119
120 assert(trigger);
121 init_notification_thread_command(&cmd);
122
123 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
124 lttng_trigger_get(trigger);
125 cmd.parameters.register_trigger.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;
133 end:
134 return ret_code;
135 }
136
137 enum lttng_error_code notification_thread_command_unregister_trigger(
138 struct notification_thread_handle *handle,
139 const struct lttng_trigger *trigger)
140 {
141 int ret;
142 enum lttng_error_code ret_code;
143 struct notification_thread_command cmd = {};
144
145 init_notification_thread_command(&cmd);
146
147 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
148 cmd.parameters.unregister_trigger.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;
156 end:
157 return ret_code;
158 }
159
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)
165 {
166 int ret;
167 enum lttng_error_code ret_code;
168 struct notification_thread_command cmd = {};
169
170 init_notification_thread_command(&cmd);
171
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;
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;
187 end:
188 return ret_code;
189 }
190
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)
194 {
195 int ret;
196 enum lttng_error_code ret_code;
197 struct notification_thread_command cmd = {};
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;
211 end:
212 return ret_code;
213 }
214
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)
219 {
220 int ret;
221 enum lttng_error_code ret_code;
222 struct notification_thread_command cmd = {};
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;
239 end:
240 return ret_code;
241 }
242
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)
248 {
249 int ret;
250 enum lttng_error_code ret_code;
251 struct notification_thread_command cmd = {};
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;
269 end:
270 return ret_code;
271 }
272
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)
277 {
278 int ret;
279 enum lttng_error_code ret_code;
280 struct notification_thread_command cmd = {};
281
282 assert(tracer_event_source_fd >= 0);
283
284 init_notification_thread_command(&cmd);
285
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;
290
291 ret = run_command_wait(handle, &cmd);
292 if (ret) {
293 ret_code = LTTNG_ERR_UNK;
294 goto end;
295 }
296
297 ret_code = cmd.reply_code;
298 end:
299 return ret_code;
300 }
301
302 enum lttng_error_code notification_thread_command_remove_tracer_event_source(
303 struct notification_thread_handle *handle,
304 int tracer_event_source_fd)
305 {
306 int ret;
307 enum lttng_error_code ret_code;
308 struct notification_thread_command cmd = {};
309
310 init_notification_thread_command(&cmd);
311
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;
315
316 ret = run_command_wait(handle, &cmd);
317 if (ret) {
318 ret_code = LTTNG_ERR_UNK;
319 goto end;
320 }
321
322 ret_code = cmd.reply_code;
323 end:
324 return ret_code;
325 }
326
327 enum lttng_error_code notification_thread_command_list_triggers(
328 struct notification_thread_handle *handle,
329 uid_t uid,
330 struct lttng_triggers **triggers)
331 {
332 int ret;
333 enum lttng_error_code ret_code;
334 struct notification_thread_command cmd = {};
335
336 assert(handle);
337 assert(triggers);
338
339 init_notification_thread_command(&cmd);
340
341 cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS;
342 cmd.parameters.list_triggers.uid = uid;
343
344 ret = run_command_wait(handle, &cmd);
345 if (ret) {
346 ret_code = LTTNG_ERR_UNK;
347 goto end;
348 }
349
350 ret_code = cmd.reply_code;
351 *triggers = cmd.reply.list_triggers.triggers;
352
353 end:
354 return ret_code;
355 }
356
357 void notification_thread_command_quit(
358 struct notification_thread_handle *handle)
359 {
360 int ret;
361 struct notification_thread_command cmd = {};
362
363 init_notification_thread_command(&cmd);
364
365 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
366 ret = run_command_wait(handle, &cmd);
367 assert(!ret && cmd.reply_code == LTTNG_OK);
368 }
369
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)
374 {
375 struct notification_thread_command cmd = {};
376
377 init_notification_thread_command(&cmd);
378
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);
383 }
384
385 /*
386 * Takes ownership of the payload if present.
387 */
388 LTTNG_HIDDEN
389 struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
390 uint64_t tracer_token,
391 enum lttng_domain_type domain,
392 char *payload,
393 size_t payload_size)
394 {
395 struct lttng_event_notifier_notification *notification = NULL;
396
397 assert(domain != LTTNG_DOMAIN_NONE);
398 assert((payload && payload_size) || (!payload && !payload_size));
399
400 notification = zmalloc(sizeof(struct lttng_event_notifier_notification));
401 if (notification == NULL) {
402 ERR("[notification-thread] Error allocating notification");
403 goto end;
404 }
405
406 notification->tracer_token = tracer_token;
407 notification->type = domain;
408 notification->capture_buffer = payload;
409 notification->capture_buf_size = payload_size;
410
411 end:
412 return notification;
413 }
414
415 LTTNG_HIDDEN
416 void lttng_event_notifier_notification_destroy(
417 struct lttng_event_notifier_notification *notification)
418 {
419 if (!notification) {
420 return;
421 }
422
423 free(notification->capture_buffer);
424 free(notification);
425 }
This page took 0.037579 seconds and 4 git commands to generate.