Commit | Line | Data |
---|---|---|
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> | |
c9e313bc SM |
10 | #include "notification-thread.hpp" |
11 | #include "notification-thread-commands.hpp" | |
12 | #include <common/error.hpp> | |
ab0ee2ca JG |
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 | { | |
ab0ee2ca | 20 | CDS_INIT_LIST_HEAD(&cmd->cmd_list_node); |
8ada111f | 21 | lttng_waiter_init(&cmd->reply_waiter); |
ab0ee2ca JG |
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 | ||
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. */ | |
f370852f | 36 | ret = lttng_write(handle->cmd_queue.event_fd, |
ab0ee2ca | 37 | ¬ification_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; |
51 | error_unlock_queue: | |
52 | pthread_mutex_unlock(&handle->cmd_queue.lock); | |
53 | return -1; | |
54 | } | |
55 | ||
0ab399e0 JG |
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 | ||
64803277 | 62 | new_cmd = zmalloc<notification_thread_command>(); |
0ab399e0 JG |
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. */ | |
f370852f | 92 | ret = lttng_write(handle->cmd_queue.event_fd, |
0ab399e0 JG |
93 | ¬ification_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 | ||
ab0ee2ca JG |
112 | enum lttng_error_code notification_thread_command_register_trigger( |
113 | struct notification_thread_handle *handle, | |
0efb2ad7 JG |
114 | struct lttng_trigger *trigger, |
115 | bool is_trigger_anonymous) | |
ab0ee2ca JG |
116 | { |
117 | int ret; | |
118 | enum lttng_error_code ret_code; | |
0ab399e0 | 119 | struct notification_thread_command cmd = {}; |
ab0ee2ca | 120 | |
a0377dfe | 121 | LTTNG_ASSERT(trigger); |
ab0ee2ca JG |
122 | init_notification_thread_command(&cmd); |
123 | ||
124 | cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER; | |
242388e4 | 125 | lttng_trigger_get(trigger); |
ac16173e | 126 | cmd.parameters.register_trigger.trigger = trigger; |
0efb2ad7 JG |
127 | cmd.parameters.register_trigger.is_trigger_anonymous = |
128 | is_trigger_anonymous; | |
ab0ee2ca JG |
129 | |
130 | ret = run_command_wait(handle, &cmd); | |
131 | if (ret) { | |
132 | ret_code = LTTNG_ERR_UNK; | |
133 | goto end; | |
134 | } | |
135 | ret_code = cmd.reply_code; | |
136 | end: | |
137 | return ret_code; | |
138 | } | |
139 | ||
140 | enum lttng_error_code notification_thread_command_unregister_trigger( | |
141 | struct notification_thread_handle *handle, | |
ac16173e | 142 | const struct lttng_trigger *trigger) |
ab0ee2ca JG |
143 | { |
144 | int ret; | |
145 | enum lttng_error_code ret_code; | |
0ab399e0 | 146 | struct notification_thread_command cmd = {}; |
ab0ee2ca JG |
147 | |
148 | init_notification_thread_command(&cmd); | |
149 | ||
150 | cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER; | |
ac16173e | 151 | cmd.parameters.unregister_trigger.trigger = trigger; |
ab0ee2ca JG |
152 | |
153 | ret = run_command_wait(handle, &cmd); | |
154 | if (ret) { | |
155 | ret_code = LTTNG_ERR_UNK; | |
156 | goto end; | |
157 | } | |
158 | ret_code = cmd.reply_code; | |
159 | end: | |
160 | return ret_code; | |
161 | } | |
162 | ||
139a8d25 JG |
163 | enum lttng_error_code notification_thread_command_add_session( |
164 | struct notification_thread_handle *handle, | |
165 | uint64_t session_id, const char *session_name, uid_t session_uid, gid_t session_gid) | |
166 | { | |
167 | int ret; | |
168 | enum lttng_error_code ret_code; | |
169 | struct notification_thread_command cmd = {}; | |
170 | ||
171 | init_notification_thread_command(&cmd); | |
172 | ||
173 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_SESSION; | |
174 | cmd.parameters.add_session.session_id = session_id; | |
175 | cmd.parameters.add_session.session_name = session_name; | |
176 | cmd.parameters.add_session.session_uid = session_uid; | |
177 | cmd.parameters.add_session.session_gid = session_gid; | |
178 | ||
179 | ret = run_command_wait(handle, &cmd); | |
180 | if (ret) { | |
181 | ret_code = LTTNG_ERR_UNK; | |
182 | goto end; | |
183 | } | |
184 | ret_code = cmd.reply_code; | |
185 | end: | |
186 | return ret_code; | |
187 | } | |
188 | ||
189 | enum lttng_error_code notification_thread_command_remove_session( | |
190 | struct notification_thread_handle *handle, | |
191 | uint64_t session_id) | |
192 | { | |
193 | int ret; | |
194 | enum lttng_error_code ret_code; | |
195 | struct notification_thread_command cmd = {}; | |
196 | ||
197 | init_notification_thread_command(&cmd); | |
198 | ||
199 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION; | |
200 | cmd.parameters.remove_session.session_id = session_id; | |
201 | ||
202 | ret = run_command_wait(handle, &cmd); | |
203 | if (ret) { | |
204 | ret_code = LTTNG_ERR_UNK; | |
205 | goto end; | |
206 | } | |
207 | ret_code = cmd.reply_code; | |
208 | end: | |
209 | return ret_code; | |
210 | } | |
211 | ||
ab0ee2ca JG |
212 | enum lttng_error_code notification_thread_command_add_channel( |
213 | struct notification_thread_handle *handle, | |
139a8d25 | 214 | uint64_t session_id, |
ab0ee2ca JG |
215 | char *channel_name, uint64_t key, |
216 | enum lttng_domain_type domain, uint64_t capacity) | |
217 | { | |
218 | int ret; | |
219 | enum lttng_error_code ret_code; | |
0ab399e0 | 220 | struct notification_thread_command cmd = {}; |
ab0ee2ca JG |
221 | |
222 | init_notification_thread_command(&cmd); | |
223 | ||
224 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL; | |
139a8d25 | 225 | cmd.parameters.add_channel.session.id = session_id; |
8abe313a JG |
226 | cmd.parameters.add_channel.channel.name = channel_name; |
227 | cmd.parameters.add_channel.channel.key = key; | |
228 | cmd.parameters.add_channel.channel.domain = domain; | |
229 | cmd.parameters.add_channel.channel.capacity = capacity; | |
ab0ee2ca JG |
230 | |
231 | ret = run_command_wait(handle, &cmd); | |
232 | if (ret) { | |
233 | ret_code = LTTNG_ERR_UNK; | |
234 | goto end; | |
235 | } | |
236 | ret_code = cmd.reply_code; | |
237 | end: | |
238 | return ret_code; | |
239 | } | |
240 | ||
241 | enum lttng_error_code notification_thread_command_remove_channel( | |
242 | struct notification_thread_handle *handle, | |
243 | uint64_t key, enum lttng_domain_type domain) | |
244 | { | |
245 | int ret; | |
246 | enum lttng_error_code ret_code; | |
0ab399e0 | 247 | struct notification_thread_command cmd = {}; |
ab0ee2ca JG |
248 | |
249 | init_notification_thread_command(&cmd); | |
250 | ||
251 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL; | |
252 | cmd.parameters.remove_channel.key = key; | |
253 | cmd.parameters.remove_channel.domain = domain; | |
254 | ||
255 | ret = run_command_wait(handle, &cmd); | |
256 | if (ret) { | |
257 | ret_code = LTTNG_ERR_UNK; | |
258 | goto end; | |
259 | } | |
260 | ret_code = cmd.reply_code; | |
261 | end: | |
262 | return ret_code; | |
263 | } | |
264 | ||
731c1b12 JG |
265 | enum lttng_error_code notification_thread_command_session_rotation_ongoing( |
266 | struct notification_thread_handle *handle, | |
139a8d25 | 267 | uint64_t session_id, |
731c1b12 JG |
268 | uint64_t trace_archive_chunk_id) |
269 | { | |
270 | int ret; | |
271 | enum lttng_error_code ret_code; | |
0ab399e0 | 272 | struct notification_thread_command cmd = {}; |
731c1b12 JG |
273 | |
274 | init_notification_thread_command(&cmd); | |
275 | ||
276 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; | |
139a8d25 | 277 | cmd.parameters.session_rotation.session_id = session_id; |
731c1b12 JG |
278 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
279 | trace_archive_chunk_id; | |
280 | ||
281 | ret = run_command_wait(handle, &cmd); | |
282 | if (ret) { | |
283 | ret_code = LTTNG_ERR_UNK; | |
284 | goto end; | |
285 | } | |
286 | ret_code = cmd.reply_code; | |
287 | end: | |
288 | return ret_code; | |
289 | } | |
290 | ||
291 | enum lttng_error_code notification_thread_command_session_rotation_completed( | |
292 | struct notification_thread_handle *handle, | |
139a8d25 | 293 | uint64_t session_id, |
731c1b12 JG |
294 | uint64_t trace_archive_chunk_id, |
295 | struct lttng_trace_archive_location *location) | |
296 | { | |
297 | int ret; | |
298 | enum lttng_error_code ret_code; | |
0ab399e0 | 299 | struct notification_thread_command cmd = {}; |
731c1b12 JG |
300 | |
301 | init_notification_thread_command(&cmd); | |
302 | ||
303 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED; | |
139a8d25 | 304 | cmd.parameters.session_rotation.session_id = session_id; |
731c1b12 JG |
305 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
306 | trace_archive_chunk_id; | |
307 | cmd.parameters.session_rotation.location = location; | |
308 | ||
309 | ret = run_command_wait(handle, &cmd); | |
310 | if (ret) { | |
311 | ret_code = LTTNG_ERR_UNK; | |
312 | goto end; | |
313 | } | |
314 | ret_code = cmd.reply_code; | |
315 | end: | |
316 | return ret_code; | |
317 | } | |
318 | ||
d02d7404 JR |
319 | enum lttng_error_code notification_thread_command_add_tracer_event_source( |
320 | struct notification_thread_handle *handle, | |
321 | int tracer_event_source_fd, | |
322 | enum lttng_domain_type domain) | |
323 | { | |
324 | int ret; | |
325 | enum lttng_error_code ret_code; | |
326 | struct notification_thread_command cmd = {}; | |
327 | ||
a0377dfe | 328 | LTTNG_ASSERT(tracer_event_source_fd >= 0); |
d02d7404 JR |
329 | |
330 | init_notification_thread_command(&cmd); | |
331 | ||
332 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE; | |
333 | cmd.parameters.tracer_event_source.tracer_event_source_fd = | |
334 | tracer_event_source_fd; | |
335 | cmd.parameters.tracer_event_source.domain = domain; | |
336 | ||
337 | ret = run_command_wait(handle, &cmd); | |
338 | if (ret) { | |
339 | ret_code = LTTNG_ERR_UNK; | |
340 | goto end; | |
341 | } | |
342 | ||
343 | ret_code = cmd.reply_code; | |
344 | end: | |
345 | return ret_code; | |
346 | } | |
347 | ||
348 | enum lttng_error_code notification_thread_command_remove_tracer_event_source( | |
349 | struct notification_thread_handle *handle, | |
350 | int tracer_event_source_fd) | |
351 | { | |
352 | int ret; | |
353 | enum lttng_error_code ret_code; | |
354 | struct notification_thread_command cmd = {}; | |
355 | ||
356 | init_notification_thread_command(&cmd); | |
357 | ||
358 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE; | |
359 | cmd.parameters.tracer_event_source.tracer_event_source_fd = | |
360 | tracer_event_source_fd; | |
361 | ||
362 | ret = run_command_wait(handle, &cmd); | |
363 | if (ret) { | |
364 | ret_code = LTTNG_ERR_UNK; | |
365 | goto end; | |
366 | } | |
367 | ||
368 | ret_code = cmd.reply_code; | |
369 | end: | |
370 | return ret_code; | |
371 | } | |
372 | ||
fbc9f37d JR |
373 | enum lttng_error_code notification_thread_command_list_triggers( |
374 | struct notification_thread_handle *handle, | |
375 | uid_t uid, | |
376 | struct lttng_triggers **triggers) | |
377 | { | |
378 | int ret; | |
379 | enum lttng_error_code ret_code; | |
380 | struct notification_thread_command cmd = {}; | |
381 | ||
a0377dfe FD |
382 | LTTNG_ASSERT(handle); |
383 | LTTNG_ASSERT(triggers); | |
fbc9f37d JR |
384 | |
385 | init_notification_thread_command(&cmd); | |
386 | ||
387 | cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS; | |
388 | cmd.parameters.list_triggers.uid = uid; | |
389 | ||
390 | ret = run_command_wait(handle, &cmd); | |
391 | if (ret) { | |
392 | ret_code = LTTNG_ERR_UNK; | |
393 | goto end; | |
394 | } | |
395 | ||
396 | ret_code = cmd.reply_code; | |
397 | *triggers = cmd.reply.list_triggers.triggers; | |
398 | ||
399 | end: | |
400 | return ret_code; | |
401 | } | |
402 | ||
ab0ee2ca JG |
403 | void notification_thread_command_quit( |
404 | struct notification_thread_handle *handle) | |
405 | { | |
406 | int ret; | |
0ab399e0 | 407 | struct notification_thread_command cmd = {}; |
ab0ee2ca JG |
408 | |
409 | init_notification_thread_command(&cmd); | |
410 | ||
411 | cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT; | |
412 | ret = run_command_wait(handle, &cmd); | |
a0377dfe | 413 | LTTNG_ASSERT(!ret && cmd.reply_code == LTTNG_OK); |
ab0ee2ca | 414 | } |
f2b3ef9f JG |
415 | |
416 | int notification_thread_client_communication_update( | |
417 | struct notification_thread_handle *handle, | |
418 | notification_client_id id, | |
419 | enum client_transmission_status transmission_status) | |
420 | { | |
421 | struct notification_thread_command cmd = {}; | |
422 | ||
423 | init_notification_thread_command(&cmd); | |
424 | ||
425 | cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE; | |
426 | cmd.parameters.client_communication_update.id = id; | |
427 | cmd.parameters.client_communication_update.status = transmission_status; | |
428 | return run_command_no_wait(handle, &cmd); | |
429 | } | |
b9a8d78f | 430 | |
8790759c FD |
431 | enum lttng_error_code notification_thread_command_get_trigger( |
432 | struct notification_thread_handle *handle, | |
433 | const struct lttng_trigger *trigger, | |
434 | struct lttng_trigger **real_trigger) | |
435 | { | |
436 | int ret; | |
437 | enum lttng_error_code ret_code; | |
438 | struct notification_thread_command cmd = {}; | |
439 | ||
440 | init_notification_thread_command(&cmd); | |
441 | ||
442 | cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TRIGGER; | |
443 | cmd.parameters.get_trigger.trigger = trigger; | |
444 | ret = run_command_wait(handle, &cmd); | |
445 | if (ret) { | |
446 | ret_code = LTTNG_ERR_UNK; | |
447 | goto end; | |
448 | } | |
449 | ||
450 | ret_code = cmd.reply_code; | |
451 | *real_trigger = cmd.reply.get_trigger.trigger; | |
452 | ||
453 | end: | |
454 | return ret_code; | |
455 | } | |
456 | ||
82b3cbf4 JR |
457 | /* |
458 | * Takes ownership of the payload if present. | |
459 | */ | |
82b3cbf4 JR |
460 | struct lttng_event_notifier_notification *lttng_event_notifier_notification_create( |
461 | uint64_t tracer_token, | |
462 | enum lttng_domain_type domain, | |
463 | char *payload, | |
464 | size_t payload_size) | |
b9a8d78f FD |
465 | { |
466 | struct lttng_event_notifier_notification *notification = NULL; | |
467 | ||
a0377dfe FD |
468 | LTTNG_ASSERT(domain != LTTNG_DOMAIN_NONE); |
469 | LTTNG_ASSERT((payload && payload_size) || (!payload && !payload_size)); | |
b9a8d78f | 470 | |
64803277 | 471 | notification = zmalloc<lttng_event_notifier_notification>(); |
b9a8d78f | 472 | if (notification == NULL) { |
bd0514a5 | 473 | ERR("Error allocating notification"); |
b9a8d78f FD |
474 | goto end; |
475 | } | |
476 | ||
477 | notification->tracer_token = tracer_token; | |
478 | notification->type = domain; | |
82b3cbf4 JR |
479 | notification->capture_buffer = payload; |
480 | notification->capture_buf_size = payload_size; | |
b9a8d78f FD |
481 | |
482 | end: | |
483 | return notification; | |
484 | } | |
485 | ||
b9a8d78f FD |
486 | void lttng_event_notifier_notification_destroy( |
487 | struct lttng_event_notifier_notification *notification) | |
488 | { | |
489 | if (!notification) { | |
490 | return; | |
491 | } | |
492 | ||
82b3cbf4 | 493 | free(notification->capture_buffer); |
b9a8d78f FD |
494 | free(notification); |
495 | } |