SoW-2019-0007-2: Dynamic Snapshot: Triggers send partial event payload with notifications
[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>
5024c2ac 13#include <common/macros.h>
ab0ee2ca
JG
14#include <unistd.h>
15#include <stdint.h>
16#include <inttypes.h>
17
18static
19void init_notification_thread_command(struct notification_thread_command *cmd)
20{
ab0ee2ca 21 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
8ada111f 22 lttng_waiter_init(&cmd->reply_waiter);
ab0ee2ca
JG
23}
24
25static
26int run_command_wait(struct notification_thread_handle *handle,
27 struct notification_thread_command *cmd)
28{
29 int ret;
30 uint64_t notification_counter = 1;
31
ab0ee2ca
JG
32 pthread_mutex_lock(&handle->cmd_queue.lock);
33 /* Add to queue. */
34 cds_list_add_tail(&cmd->cmd_list_node,
35 &handle->cmd_queue.list);
36 /* Wake-up thread. */
58d3fed5 37 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
ab0ee2ca 38 &notification_counter, sizeof(notification_counter));
58d3fed5 39 if (ret != sizeof(notification_counter)) {
ab0ee2ca
JG
40 PERROR("write to notification thread's queue event fd");
41 /*
42 * Remove the command from the list so the notification
43 * thread does not process it.
44 */
45 cds_list_del(&cmd->cmd_list_node);
46 goto error_unlock_queue;
47 }
48 pthread_mutex_unlock(&handle->cmd_queue.lock);
49
8ada111f 50 lttng_waiter_wait(&cmd->reply_waiter);
ab0ee2ca
JG
51 return 0;
52error_unlock_queue:
53 pthread_mutex_unlock(&handle->cmd_queue.lock);
54 return -1;
55}
56
5024c2ac
JR
57static
58struct notification_thread_command *notification_thread_command_copy(
59 const struct notification_thread_command *original_cmd)
60{
61 struct notification_thread_command *new_cmd;
62
63 new_cmd = zmalloc(sizeof(*new_cmd));
64 if (!new_cmd) {
65 goto end;
66 }
67
68 *new_cmd = *original_cmd;
69 init_notification_thread_command(new_cmd);
70end:
71 return new_cmd;
72}
73
74static
75int run_command_no_wait(struct notification_thread_handle *handle,
76 const struct notification_thread_command *in_cmd)
77{
78 int ret;
79 uint64_t notification_counter = 1;
80 struct notification_thread_command *new_cmd =
81 notification_thread_command_copy(in_cmd);
82
83 if (!new_cmd) {
84 goto error;
85 }
86 new_cmd->is_async = true;
87
88 pthread_mutex_lock(&handle->cmd_queue.lock);
89 /* Add to queue. */
90 cds_list_add_tail(&new_cmd->cmd_list_node,
91 &handle->cmd_queue.list);
92 /* Wake-up thread. */
93 ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe),
94 &notification_counter, sizeof(notification_counter));
95 if (ret != sizeof(notification_counter)) {
96 PERROR("write to notification thread's queue event fd");
97 /*
98 * Remove the command from the list so the notification
99 * thread does not process it.
100 */
101 cds_list_del(&new_cmd->cmd_list_node);
102 goto error_unlock_queue;
103 }
104 pthread_mutex_unlock(&handle->cmd_queue.lock);
105 return 0;
106error_unlock_queue:
107 free(new_cmd);
108 pthread_mutex_unlock(&handle->cmd_queue.lock);
109error:
110 return -1;
111}
112
ab0ee2ca
JG
113enum lttng_error_code notification_thread_command_register_trigger(
114 struct notification_thread_handle *handle,
115 struct lttng_trigger *trigger)
116{
117 int ret;
118 enum lttng_error_code ret_code;
5024c2ac 119 struct notification_thread_command cmd = {};
ab0ee2ca
JG
120
121 init_notification_thread_command(&cmd);
122
123 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
124 cmd.parameters.trigger = trigger;
125
126 ret = run_command_wait(handle, &cmd);
127 if (ret) {
128 ret_code = LTTNG_ERR_UNK;
129 goto end;
130 }
131 ret_code = cmd.reply_code;
132end:
133 return ret_code;
134}
135
136enum lttng_error_code notification_thread_command_unregister_trigger(
137 struct notification_thread_handle *handle,
138 struct lttng_trigger *trigger)
139{
140 int ret;
141 enum lttng_error_code ret_code;
5024c2ac 142 struct notification_thread_command cmd = {};
ab0ee2ca
JG
143
144 init_notification_thread_command(&cmd);
145
146 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
147 cmd.parameters.trigger = trigger;
148
149 ret = run_command_wait(handle, &cmd);
150 if (ret) {
151 ret_code = LTTNG_ERR_UNK;
152 goto end;
153 }
154 ret_code = cmd.reply_code;
155end:
156 return ret_code;
157}
158
159enum lttng_error_code notification_thread_command_add_channel(
160 struct notification_thread_handle *handle,
161 char *session_name, uid_t uid, gid_t gid,
162 char *channel_name, uint64_t key,
163 enum lttng_domain_type domain, uint64_t capacity)
164{
165 int ret;
166 enum lttng_error_code ret_code;
5024c2ac 167 struct notification_thread_command cmd = {};
ab0ee2ca
JG
168
169 init_notification_thread_command(&cmd);
170
171 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
8abe313a
JG
172 cmd.parameters.add_channel.session.name = session_name;
173 cmd.parameters.add_channel.session.uid = uid;
174 cmd.parameters.add_channel.session.gid = gid;
175 cmd.parameters.add_channel.channel.name = channel_name;
176 cmd.parameters.add_channel.channel.key = key;
177 cmd.parameters.add_channel.channel.domain = domain;
178 cmd.parameters.add_channel.channel.capacity = capacity;
ab0ee2ca
JG
179
180 ret = run_command_wait(handle, &cmd);
181 if (ret) {
182 ret_code = LTTNG_ERR_UNK;
183 goto end;
184 }
185 ret_code = cmd.reply_code;
186end:
187 return ret_code;
188}
189
190enum lttng_error_code notification_thread_command_remove_channel(
191 struct notification_thread_handle *handle,
192 uint64_t key, enum lttng_domain_type domain)
193{
194 int ret;
195 enum lttng_error_code ret_code;
5024c2ac 196 struct notification_thread_command cmd = {};
ab0ee2ca
JG
197
198 init_notification_thread_command(&cmd);
199
200 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
201 cmd.parameters.remove_channel.key = key;
202 cmd.parameters.remove_channel.domain = domain;
203
204 ret = run_command_wait(handle, &cmd);
205 if (ret) {
206 ret_code = LTTNG_ERR_UNK;
207 goto end;
208 }
209 ret_code = cmd.reply_code;
210end:
211 return ret_code;
212}
213
731c1b12
JG
214enum lttng_error_code notification_thread_command_session_rotation_ongoing(
215 struct notification_thread_handle *handle,
216 const char *session_name, uid_t uid, gid_t gid,
217 uint64_t trace_archive_chunk_id)
218{
219 int ret;
220 enum lttng_error_code ret_code;
5024c2ac 221 struct notification_thread_command cmd = {};
731c1b12
JG
222
223 init_notification_thread_command(&cmd);
224
225 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
226 cmd.parameters.session_rotation.session_name = session_name;
227 cmd.parameters.session_rotation.uid = uid;
228 cmd.parameters.session_rotation.gid = gid;
229 cmd.parameters.session_rotation.trace_archive_chunk_id =
230 trace_archive_chunk_id;
231
232 ret = run_command_wait(handle, &cmd);
233 if (ret) {
234 ret_code = LTTNG_ERR_UNK;
235 goto end;
236 }
237 ret_code = cmd.reply_code;
238end:
239 return ret_code;
240}
241
242enum lttng_error_code notification_thread_command_session_rotation_completed(
243 struct notification_thread_handle *handle,
244 const char *session_name, uid_t uid, gid_t gid,
245 uint64_t trace_archive_chunk_id,
246 struct lttng_trace_archive_location *location)
247{
248 int ret;
249 enum lttng_error_code ret_code;
5024c2ac 250 struct notification_thread_command cmd = {};
731c1b12
JG
251
252 init_notification_thread_command(&cmd);
253
254 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED;
255 cmd.parameters.session_rotation.session_name = session_name;
256 cmd.parameters.session_rotation.uid = uid;
257 cmd.parameters.session_rotation.gid = gid;
258 cmd.parameters.session_rotation.trace_archive_chunk_id =
259 trace_archive_chunk_id;
260 cmd.parameters.session_rotation.location = location;
261
262 ret = run_command_wait(handle, &cmd);
263 if (ret) {
264 ret_code = LTTNG_ERR_UNK;
265 goto end;
266 }
267 ret_code = cmd.reply_code;
268end:
269 return ret_code;
270}
271
5024c2ac
JR
272enum lttng_error_code notification_thread_command_add_application(
273 struct notification_thread_handle *handle,
274 struct lttng_pipe *pipe)
275{
276 int ret;
277 enum lttng_error_code ret_code;
278 struct notification_thread_command cmd = {};
279
280 init_notification_thread_command(&cmd);
281
282 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION;
283 cmd.parameters.application.read_side_trigger_event_application_pipe = lttng_pipe_get_readfd(pipe);
284
285 ret = run_command_wait(handle, &cmd);
286 if (ret) {
287 ret_code = LTTNG_ERR_UNK;
288 goto end;
289 }
290 ret_code = cmd.reply_code;
291end:
292 return ret_code;
293}
294
295enum lttng_error_code notification_thread_command_remove_application(
296 struct notification_thread_handle *handle,
297 struct lttng_pipe *pipe)
298{
299 int ret;
300 enum lttng_error_code ret_code;
301 struct notification_thread_command cmd = {};
302
303 init_notification_thread_command(&cmd);
304
305 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION;
306 cmd.parameters.application.read_side_trigger_event_application_pipe = lttng_pipe_get_readfd(pipe);
307
308 ret = run_command_wait(handle, &cmd);
309 if (ret) {
310 ret_code = LTTNG_ERR_UNK;
311 goto end;
312 }
313 ret_code = cmd.reply_code;
314end:
315 return ret_code;
316}
317
318enum lttng_error_code notification_thread_command_get_tokens(
319 struct notification_thread_handle *handle,
320 struct lttng_triggers **tokens_triggers)
321{
322 int ret;
323 enum lttng_error_code ret_code;
324 struct notification_thread_command cmd = {};
325
326 assert(handle);
327 assert(tokens_triggers);
328
329 init_notification_thread_command(&cmd);
330
331 cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TOKENS;
332
333 ret = run_command_wait(handle, &cmd);
334 if (ret) {
335 ret_code = LTTNG_ERR_UNK;
336 goto end;
337 }
338 ret_code = cmd.reply_code;
339 *tokens_triggers = cmd.reply.get_tokens.triggers;
340
341end:
342 return ret_code;
343}
344
345enum lttng_error_code notification_thread_command_list_triggers(
346 struct notification_thread_handle *handle,
347 uid_t uid,
348 gid_t gid,
349 struct lttng_triggers **triggers)
350{
351 int ret;
352 enum lttng_error_code ret_code;
353 struct notification_thread_command cmd = {};
354
355 assert(handle);
356 assert(triggers);
357
358 init_notification_thread_command(&cmd);
359
360 cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS;
361 cmd.parameters.list_triggers.uid = uid;
362 cmd.parameters.list_triggers.gid = gid;
363
364 ret = run_command_wait(handle, &cmd);
365 if (ret) {
366 ret_code = LTTNG_ERR_UNK;
367 goto end;
368 }
369 ret_code = cmd.reply_code;
370 *triggers = cmd.reply.list_triggers.triggers;
371
372end:
373 return ret_code;
374}
375
ab0ee2ca
JG
376void notification_thread_command_quit(
377 struct notification_thread_handle *handle)
378{
379 int ret;
5024c2ac 380 struct notification_thread_command cmd = {};
ab0ee2ca
JG
381
382 init_notification_thread_command(&cmd);
383
384 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
385 ret = run_command_wait(handle, &cmd);
386 assert(!ret && cmd.reply_code == LTTNG_OK);
387}
5024c2ac
JR
388
389int notification_thread_client_communication_update(
390 struct notification_thread_handle *handle,
391 notification_client_id id,
392 enum client_transmission_status transmission_status)
393{
394 struct notification_thread_command cmd = {};
395
396 init_notification_thread_command(&cmd);
397
398 cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE;
399 cmd.parameters.client_communication_update.id = id;
400 cmd.parameters.client_communication_update.status = transmission_status;
401 return run_command_no_wait(handle, &cmd);
402}
403
404/*
405 * Takes ownership of the payload if present.
406 */
407LTTNG_HIDDEN
408struct lttng_trigger_notification *lttng_trigger_notification_create(
409 uint64_t id,
410 enum lttng_domain_type domain,
411 char *payload,
412 size_t payload_size)
413{
414 struct lttng_trigger_notification *notification = NULL;
415
416 assert(domain != LTTNG_DOMAIN_NONE);
417
418 if (payload) {
419 assert(payload_size > 0);
420 } else {
421 assert(payload_size == 0);
422 }
423
424 notification = zmalloc(sizeof(struct lttng_trigger_notification));
425 if (notification == NULL) {
426 ERR("[notification-thread] Error allocating notification ");
427 goto end;
428 }
429
430 notification->id = id;
431 notification->type = domain;
432 notification->capture_buffer = payload;
433 notification->capture_buf_size = payload_size;
434
435end:
436 return notification;
437}
438
439LTTNG_HIDDEN
440void lttng_trigger_notification_destroy(
441 struct lttng_trigger_notification *notification)
442{
443 if (!notification) {
444 return;
445 }
446
447 if(notification->capture_buffer) {
448 free(notification->capture_buffer);
449 }
450 free(notification);
451}
This page took 0.062317 seconds and 5 git commands to generate.