fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-commands.cpp
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 "notification-thread-commands.hpp"
9 #include "notification-thread.hpp"
10
11 #include <common/error.hpp>
12
13 #include <lttng/lttng-error.h>
14 #include <lttng/trigger/trigger.h>
15
16 #include <inttypes.h>
17 #include <stdint.h>
18 #include <unistd.h>
19
20 static void init_notification_thread_command(struct notification_thread_command *cmd)
21 {
22 CDS_INIT_LIST_HEAD(&cmd->cmd_list_node);
23 }
24
25 static 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 lttng::synchro::waiter command_completion_waiter;
32 cmd->command_completed_waker.emplace(command_completion_waiter.get_waker());
33
34 pthread_mutex_lock(&handle->cmd_queue.lock);
35 /* Add to queue. */
36 cds_list_add_tail(&cmd->cmd_list_node, &handle->cmd_queue.list);
37 /* Wake-up thread. */
38 ret = lttng_write(
39 handle->cmd_queue.event_fd, &notification_counter, sizeof(notification_counter));
40 if (ret != sizeof(notification_counter)) {
41 PERROR("write to notification thread's queue event fd");
42 /*
43 * Remove the command from the list so the notification
44 * thread does not process it.
45 */
46 cds_list_del(&cmd->cmd_list_node);
47 goto error_unlock_queue;
48 }
49 pthread_mutex_unlock(&handle->cmd_queue.lock);
50
51 command_completion_waiter.wait();
52 ;
53 return 0;
54 error_unlock_queue:
55 pthread_mutex_unlock(&handle->cmd_queue.lock);
56 return -1;
57 }
58
59 static struct notification_thread_command *
60 notification_thread_command_copy(const struct notification_thread_command *original_cmd)
61 {
62 struct notification_thread_command *new_cmd;
63
64 try {
65 new_cmd = new notification_thread_command;
66 } catch (const std::bad_alloc& e) {
67 ERR("Failed to allocate notification_thread_command: %s", e.what());
68 return nullptr;
69 }
70
71 *new_cmd = *original_cmd;
72 init_notification_thread_command(new_cmd);
73 return new_cmd;
74 }
75
76 static int run_command_no_wait(struct notification_thread_handle *handle,
77 const struct notification_thread_command *in_cmd)
78 {
79 int ret;
80 uint64_t notification_counter = 1;
81 struct notification_thread_command *new_cmd = 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, &handle->cmd_queue.list);
91 /* Wake-up thread. */
92 ret = lttng_write(
93 handle->cmd_queue.event_fd, &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
104 pthread_mutex_unlock(&handle->cmd_queue.lock);
105 return 0;
106 error_unlock_queue:
107
108 delete new_cmd;
109 pthread_mutex_unlock(&handle->cmd_queue.lock);
110 error:
111 return -1;
112 }
113
114 enum lttng_error_code
115 notification_thread_command_register_trigger(struct notification_thread_handle *handle,
116 struct lttng_trigger *trigger,
117 bool is_trigger_anonymous)
118 {
119 int ret;
120 enum lttng_error_code ret_code;
121 notification_thread_command cmd;
122
123 LTTNG_ASSERT(trigger);
124 init_notification_thread_command(&cmd);
125
126 cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
127 lttng_trigger_get(trigger);
128 cmd.parameters.register_trigger.trigger = trigger;
129 cmd.parameters.register_trigger.is_trigger_anonymous = is_trigger_anonymous;
130
131 ret = run_command_wait(handle, &cmd);
132 if (ret) {
133 ret_code = LTTNG_ERR_UNK;
134 goto end;
135 }
136 ret_code = cmd.reply_code;
137 end:
138 return ret_code;
139 }
140
141 enum lttng_error_code
142 notification_thread_command_unregister_trigger(struct notification_thread_handle *handle,
143 const struct lttng_trigger *trigger)
144 {
145 int ret;
146 enum lttng_error_code ret_code;
147 notification_thread_command cmd;
148
149 init_notification_thread_command(&cmd);
150
151 cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER;
152 cmd.parameters.unregister_trigger.trigger = trigger;
153
154 ret = run_command_wait(handle, &cmd);
155 if (ret) {
156 ret_code = LTTNG_ERR_UNK;
157 goto end;
158 }
159 ret_code = cmd.reply_code;
160 end:
161 return ret_code;
162 }
163
164 enum lttng_error_code
165 notification_thread_command_add_session(struct notification_thread_handle *handle,
166 uint64_t session_id,
167 const char *session_name,
168 uid_t session_uid,
169 gid_t session_gid)
170 {
171 int ret;
172 enum lttng_error_code ret_code;
173 notification_thread_command cmd;
174
175 init_notification_thread_command(&cmd);
176
177 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_SESSION;
178 cmd.parameters.add_session.session_id = session_id;
179 cmd.parameters.add_session.session_name = session_name;
180 cmd.parameters.add_session.session_uid = session_uid;
181 cmd.parameters.add_session.session_gid = session_gid;
182
183 ret = run_command_wait(handle, &cmd);
184 if (ret) {
185 ret_code = LTTNG_ERR_UNK;
186 goto end;
187 }
188 ret_code = cmd.reply_code;
189 end:
190 return ret_code;
191 }
192
193 enum lttng_error_code
194 notification_thread_command_remove_session(struct notification_thread_handle *handle,
195 uint64_t session_id)
196 {
197 int ret;
198 enum lttng_error_code ret_code;
199 notification_thread_command cmd;
200
201 init_notification_thread_command(&cmd);
202
203 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION;
204 cmd.parameters.remove_session.session_id = session_id;
205
206 ret = run_command_wait(handle, &cmd);
207 if (ret) {
208 ret_code = LTTNG_ERR_UNK;
209 goto end;
210 }
211 ret_code = cmd.reply_code;
212 end:
213 return ret_code;
214 }
215
216 enum lttng_error_code
217 notification_thread_command_add_channel(struct notification_thread_handle *handle,
218 uint64_t session_id,
219 char *channel_name,
220 uint64_t key,
221 enum lttng_domain_type domain,
222 uint64_t capacity)
223 {
224 int ret;
225 enum lttng_error_code ret_code;
226 notification_thread_command cmd;
227
228 init_notification_thread_command(&cmd);
229
230 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL;
231 cmd.parameters.add_channel.session.id = session_id;
232 cmd.parameters.add_channel.channel.name = channel_name;
233 cmd.parameters.add_channel.channel.key = key;
234 cmd.parameters.add_channel.channel.domain = domain;
235 cmd.parameters.add_channel.channel.capacity = capacity;
236
237 ret = run_command_wait(handle, &cmd);
238 if (ret) {
239 ret_code = LTTNG_ERR_UNK;
240 goto end;
241 }
242 ret_code = cmd.reply_code;
243 end:
244 return ret_code;
245 }
246
247 enum lttng_error_code notification_thread_command_remove_channel(
248 struct notification_thread_handle *handle, uint64_t key, enum lttng_domain_type domain)
249 {
250 int ret;
251 enum lttng_error_code ret_code;
252 notification_thread_command cmd;
253
254 init_notification_thread_command(&cmd);
255
256 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL;
257 cmd.parameters.remove_channel.key = key;
258 cmd.parameters.remove_channel.domain = domain;
259
260 ret = run_command_wait(handle, &cmd);
261 if (ret) {
262 ret_code = LTTNG_ERR_UNK;
263 goto end;
264 }
265 ret_code = cmd.reply_code;
266 end:
267 return ret_code;
268 }
269
270 enum lttng_error_code
271 notification_thread_command_session_rotation_ongoing(struct notification_thread_handle *handle,
272 uint64_t session_id,
273 uint64_t trace_archive_chunk_id)
274 {
275 int ret;
276 enum lttng_error_code ret_code;
277 notification_thread_command cmd;
278
279 init_notification_thread_command(&cmd);
280
281 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
282 cmd.parameters.session_rotation.session_id = session_id;
283 cmd.parameters.session_rotation.trace_archive_chunk_id = trace_archive_chunk_id;
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;
291 end:
292 return ret_code;
293 }
294
295 enum lttng_error_code notification_thread_command_session_rotation_completed(
296 struct notification_thread_handle *handle,
297 uint64_t session_id,
298 uint64_t trace_archive_chunk_id,
299 struct lttng_trace_archive_location *location)
300 {
301 int ret;
302 enum lttng_error_code ret_code;
303 notification_thread_command cmd;
304
305 init_notification_thread_command(&cmd);
306
307 cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED;
308 cmd.parameters.session_rotation.session_id = session_id;
309 cmd.parameters.session_rotation.trace_archive_chunk_id = trace_archive_chunk_id;
310 cmd.parameters.session_rotation.location = location;
311
312 ret = run_command_wait(handle, &cmd);
313 if (ret) {
314 ret_code = LTTNG_ERR_UNK;
315 goto end;
316 }
317 ret_code = cmd.reply_code;
318 end:
319 return ret_code;
320 }
321
322 enum lttng_error_code
323 notification_thread_command_add_tracer_event_source(struct notification_thread_handle *handle,
324 int tracer_event_source_fd,
325 enum lttng_domain_type domain)
326 {
327 int ret;
328 enum lttng_error_code ret_code;
329 notification_thread_command cmd;
330
331 LTTNG_ASSERT(tracer_event_source_fd >= 0);
332
333 init_notification_thread_command(&cmd);
334
335 cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE;
336 cmd.parameters.tracer_event_source.tracer_event_source_fd = tracer_event_source_fd;
337 cmd.parameters.tracer_event_source.domain = domain;
338
339 ret = run_command_wait(handle, &cmd);
340 if (ret) {
341 ret_code = LTTNG_ERR_UNK;
342 goto end;
343 }
344
345 ret_code = cmd.reply_code;
346 end:
347 return ret_code;
348 }
349
350 enum lttng_error_code
351 notification_thread_command_remove_tracer_event_source(struct notification_thread_handle *handle,
352 int tracer_event_source_fd)
353 {
354 int ret;
355 enum lttng_error_code ret_code;
356 notification_thread_command cmd;
357
358 init_notification_thread_command(&cmd);
359
360 cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE;
361 cmd.parameters.tracer_event_source.tracer_event_source_fd = tracer_event_source_fd;
362
363 ret = run_command_wait(handle, &cmd);
364 if (ret) {
365 ret_code = LTTNG_ERR_UNK;
366 goto end;
367 }
368
369 ret_code = cmd.reply_code;
370 end:
371 return ret_code;
372 }
373
374 enum lttng_error_code notification_thread_command_list_triggers(
375 struct notification_thread_handle *handle, uid_t uid, struct lttng_triggers **triggers)
376 {
377 int ret;
378 enum lttng_error_code ret_code;
379 notification_thread_command cmd;
380
381 LTTNG_ASSERT(handle);
382 LTTNG_ASSERT(triggers);
383
384 init_notification_thread_command(&cmd);
385
386 cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS;
387 cmd.parameters.list_triggers.uid = uid;
388
389 ret = run_command_wait(handle, &cmd);
390 if (ret) {
391 ret_code = LTTNG_ERR_UNK;
392 goto end;
393 }
394
395 ret_code = cmd.reply_code;
396 *triggers = cmd.reply.list_triggers.triggers;
397
398 end:
399 return ret_code;
400 }
401
402 void notification_thread_command_quit(struct notification_thread_handle *handle)
403 {
404 int ret;
405 notification_thread_command cmd;
406
407 init_notification_thread_command(&cmd);
408
409 cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT;
410 ret = run_command_wait(handle, &cmd);
411 LTTNG_ASSERT(!ret && cmd.reply_code == LTTNG_OK);
412 }
413
414 int notification_thread_client_communication_update(
415 struct notification_thread_handle *handle,
416 notification_client_id id,
417 enum client_transmission_status transmission_status)
418 {
419 notification_thread_command cmd;
420
421 init_notification_thread_command(&cmd);
422
423 cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE;
424 cmd.parameters.client_communication_update.id = id;
425 cmd.parameters.client_communication_update.status = transmission_status;
426 return run_command_no_wait(handle, &cmd);
427 }
428
429 enum lttng_error_code
430 notification_thread_command_get_trigger(struct notification_thread_handle *handle,
431 const struct lttng_trigger *trigger,
432 struct lttng_trigger **real_trigger)
433 {
434 int ret;
435 enum lttng_error_code ret_code;
436 notification_thread_command cmd;
437
438 init_notification_thread_command(&cmd);
439
440 cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TRIGGER;
441 cmd.parameters.get_trigger.trigger = trigger;
442 ret = run_command_wait(handle, &cmd);
443 if (ret) {
444 ret_code = LTTNG_ERR_UNK;
445 goto end;
446 }
447
448 ret_code = cmd.reply_code;
449 *real_trigger = cmd.reply.get_trigger.trigger;
450
451 end:
452 return ret_code;
453 }
454
455 /*
456 * Takes ownership of the payload if present.
457 */
458 struct lttng_event_notifier_notification *lttng_event_notifier_notification_create(
459 uint64_t tracer_token, enum lttng_domain_type domain, char *payload, size_t payload_size)
460 {
461 struct lttng_event_notifier_notification *notification = nullptr;
462
463 LTTNG_ASSERT(domain != LTTNG_DOMAIN_NONE);
464 LTTNG_ASSERT((payload && payload_size) || (!payload && !payload_size));
465
466 notification = zmalloc<lttng_event_notifier_notification>();
467 if (notification == nullptr) {
468 ERR("Error allocating notification");
469 goto end;
470 }
471
472 notification->tracer_token = tracer_token;
473 notification->type = domain;
474 notification->capture_buffer = payload;
475 notification->capture_buf_size = payload_size;
476
477 end:
478 return notification;
479 }
480
481 void lttng_event_notifier_notification_destroy(
482 struct lttng_event_notifier_notification *notification)
483 {
484 if (!notification) {
485 return;
486 }
487
488 free(notification->capture_buffer);
489 free(notification);
490 }
This page took 0.038276 seconds and 4 git commands to generate.