| 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 | lttng_waiter_init(&cmd->reply_waiter); |
| 24 | } |
| 25 | |
| 26 | static int 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 | |
| 32 | pthread_mutex_lock(&handle->cmd_queue.lock); |
| 33 | /* Add to queue. */ |
| 34 | cds_list_add_tail(&cmd->cmd_list_node, &handle->cmd_queue.list); |
| 35 | /* Wake-up thread. */ |
| 36 | ret = lttng_write( |
| 37 | handle->cmd_queue.event_fd, ¬ification_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 struct notification_thread_command * |
| 57 | notification_thread_command_copy(const struct notification_thread_command *original_cmd) |
| 58 | { |
| 59 | struct notification_thread_command *new_cmd; |
| 60 | |
| 61 | new_cmd = zmalloc<notification_thread_command>(); |
| 62 | if (!new_cmd) { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | *new_cmd = *original_cmd; |
| 67 | init_notification_thread_command(new_cmd); |
| 68 | end: |
| 69 | return new_cmd; |
| 70 | } |
| 71 | |
| 72 | static int run_command_no_wait(struct notification_thread_handle *handle, |
| 73 | const struct notification_thread_command *in_cmd) |
| 74 | { |
| 75 | int ret; |
| 76 | uint64_t notification_counter = 1; |
| 77 | struct notification_thread_command *new_cmd = notification_thread_command_copy(in_cmd); |
| 78 | |
| 79 | if (!new_cmd) { |
| 80 | goto error; |
| 81 | } |
| 82 | new_cmd->is_async = true; |
| 83 | |
| 84 | pthread_mutex_lock(&handle->cmd_queue.lock); |
| 85 | /* Add to queue. */ |
| 86 | cds_list_add_tail(&new_cmd->cmd_list_node, &handle->cmd_queue.list); |
| 87 | /* Wake-up thread. */ |
| 88 | ret = lttng_write( |
| 89 | handle->cmd_queue.event_fd, ¬ification_counter, sizeof(notification_counter)); |
| 90 | if (ret != sizeof(notification_counter)) { |
| 91 | PERROR("write to notification thread's queue event fd"); |
| 92 | /* |
| 93 | * Remove the command from the list so the notification |
| 94 | * thread does not process it. |
| 95 | */ |
| 96 | cds_list_del(&new_cmd->cmd_list_node); |
| 97 | goto error_unlock_queue; |
| 98 | } |
| 99 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 100 | return 0; |
| 101 | error_unlock_queue: |
| 102 | free(new_cmd); |
| 103 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 104 | error: |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | enum lttng_error_code |
| 109 | notification_thread_command_register_trigger(struct notification_thread_handle *handle, |
| 110 | struct lttng_trigger *trigger, |
| 111 | bool is_trigger_anonymous) |
| 112 | { |
| 113 | int ret; |
| 114 | enum lttng_error_code ret_code; |
| 115 | struct notification_thread_command cmd = {}; |
| 116 | |
| 117 | LTTNG_ASSERT(trigger); |
| 118 | init_notification_thread_command(&cmd); |
| 119 | |
| 120 | cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER; |
| 121 | lttng_trigger_get(trigger); |
| 122 | cmd.parameters.register_trigger.trigger = trigger; |
| 123 | cmd.parameters.register_trigger.is_trigger_anonymous = is_trigger_anonymous; |
| 124 | |
| 125 | ret = run_command_wait(handle, &cmd); |
| 126 | if (ret) { |
| 127 | ret_code = LTTNG_ERR_UNK; |
| 128 | goto end; |
| 129 | } |
| 130 | ret_code = cmd.reply_code; |
| 131 | end: |
| 132 | return ret_code; |
| 133 | } |
| 134 | |
| 135 | enum lttng_error_code |
| 136 | notification_thread_command_unregister_trigger(struct notification_thread_handle *handle, |
| 137 | const struct lttng_trigger *trigger) |
| 138 | { |
| 139 | int ret; |
| 140 | enum lttng_error_code ret_code; |
| 141 | struct notification_thread_command cmd = {}; |
| 142 | |
| 143 | init_notification_thread_command(&cmd); |
| 144 | |
| 145 | cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER; |
| 146 | cmd.parameters.unregister_trigger.trigger = trigger; |
| 147 | |
| 148 | ret = run_command_wait(handle, &cmd); |
| 149 | if (ret) { |
| 150 | ret_code = LTTNG_ERR_UNK; |
| 151 | goto end; |
| 152 | } |
| 153 | ret_code = cmd.reply_code; |
| 154 | end: |
| 155 | return ret_code; |
| 156 | } |
| 157 | |
| 158 | enum lttng_error_code |
| 159 | notification_thread_command_add_session(struct notification_thread_handle *handle, |
| 160 | uint64_t session_id, |
| 161 | const char *session_name, |
| 162 | uid_t session_uid, |
| 163 | gid_t session_gid) |
| 164 | { |
| 165 | int ret; |
| 166 | enum lttng_error_code ret_code; |
| 167 | struct notification_thread_command cmd = {}; |
| 168 | |
| 169 | init_notification_thread_command(&cmd); |
| 170 | |
| 171 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_SESSION; |
| 172 | cmd.parameters.add_session.session_id = session_id; |
| 173 | cmd.parameters.add_session.session_name = session_name; |
| 174 | cmd.parameters.add_session.session_uid = session_uid; |
| 175 | cmd.parameters.add_session.session_gid = session_gid; |
| 176 | |
| 177 | ret = run_command_wait(handle, &cmd); |
| 178 | if (ret) { |
| 179 | ret_code = LTTNG_ERR_UNK; |
| 180 | goto end; |
| 181 | } |
| 182 | ret_code = cmd.reply_code; |
| 183 | end: |
| 184 | return ret_code; |
| 185 | } |
| 186 | |
| 187 | enum lttng_error_code |
| 188 | notification_thread_command_remove_session(struct notification_thread_handle *handle, |
| 189 | uint64_t session_id) |
| 190 | { |
| 191 | int ret; |
| 192 | enum lttng_error_code ret_code; |
| 193 | struct notification_thread_command cmd = {}; |
| 194 | |
| 195 | init_notification_thread_command(&cmd); |
| 196 | |
| 197 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION; |
| 198 | cmd.parameters.remove_session.session_id = session_id; |
| 199 | |
| 200 | ret = run_command_wait(handle, &cmd); |
| 201 | if (ret) { |
| 202 | ret_code = LTTNG_ERR_UNK; |
| 203 | goto end; |
| 204 | } |
| 205 | ret_code = cmd.reply_code; |
| 206 | end: |
| 207 | return ret_code; |
| 208 | } |
| 209 | |
| 210 | enum lttng_error_code |
| 211 | notification_thread_command_add_channel(struct notification_thread_handle *handle, |
| 212 | uint64_t session_id, |
| 213 | char *channel_name, |
| 214 | uint64_t key, |
| 215 | enum lttng_domain_type domain, |
| 216 | uint64_t capacity) |
| 217 | { |
| 218 | int ret; |
| 219 | enum lttng_error_code ret_code; |
| 220 | struct notification_thread_command cmd = {}; |
| 221 | |
| 222 | init_notification_thread_command(&cmd); |
| 223 | |
| 224 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL; |
| 225 | cmd.parameters.add_channel.session.id = session_id; |
| 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; |
| 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, uint64_t key, enum lttng_domain_type domain) |
| 243 | { |
| 244 | int ret; |
| 245 | enum lttng_error_code ret_code; |
| 246 | struct notification_thread_command cmd = {}; |
| 247 | |
| 248 | init_notification_thread_command(&cmd); |
| 249 | |
| 250 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL; |
| 251 | cmd.parameters.remove_channel.key = key; |
| 252 | cmd.parameters.remove_channel.domain = domain; |
| 253 | |
| 254 | ret = run_command_wait(handle, &cmd); |
| 255 | if (ret) { |
| 256 | ret_code = LTTNG_ERR_UNK; |
| 257 | goto end; |
| 258 | } |
| 259 | ret_code = cmd.reply_code; |
| 260 | end: |
| 261 | return ret_code; |
| 262 | } |
| 263 | |
| 264 | enum lttng_error_code |
| 265 | notification_thread_command_session_rotation_ongoing(struct notification_thread_handle *handle, |
| 266 | uint64_t session_id, |
| 267 | uint64_t trace_archive_chunk_id) |
| 268 | { |
| 269 | int ret; |
| 270 | enum lttng_error_code ret_code; |
| 271 | struct notification_thread_command cmd = {}; |
| 272 | |
| 273 | init_notification_thread_command(&cmd); |
| 274 | |
| 275 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; |
| 276 | cmd.parameters.session_rotation.session_id = session_id; |
| 277 | cmd.parameters.session_rotation.trace_archive_chunk_id = trace_archive_chunk_id; |
| 278 | |
| 279 | ret = run_command_wait(handle, &cmd); |
| 280 | if (ret) { |
| 281 | ret_code = LTTNG_ERR_UNK; |
| 282 | goto end; |
| 283 | } |
| 284 | ret_code = cmd.reply_code; |
| 285 | end: |
| 286 | return ret_code; |
| 287 | } |
| 288 | |
| 289 | enum lttng_error_code notification_thread_command_session_rotation_completed( |
| 290 | struct notification_thread_handle *handle, |
| 291 | uint64_t session_id, |
| 292 | uint64_t trace_archive_chunk_id, |
| 293 | struct lttng_trace_archive_location *location) |
| 294 | { |
| 295 | int ret; |
| 296 | enum lttng_error_code ret_code; |
| 297 | struct notification_thread_command cmd = {}; |
| 298 | |
| 299 | init_notification_thread_command(&cmd); |
| 300 | |
| 301 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED; |
| 302 | cmd.parameters.session_rotation.session_id = session_id; |
| 303 | cmd.parameters.session_rotation.trace_archive_chunk_id = trace_archive_chunk_id; |
| 304 | cmd.parameters.session_rotation.location = location; |
| 305 | |
| 306 | ret = run_command_wait(handle, &cmd); |
| 307 | if (ret) { |
| 308 | ret_code = LTTNG_ERR_UNK; |
| 309 | goto end; |
| 310 | } |
| 311 | ret_code = cmd.reply_code; |
| 312 | end: |
| 313 | return ret_code; |
| 314 | } |
| 315 | |
| 316 | enum lttng_error_code |
| 317 | notification_thread_command_add_tracer_event_source(struct notification_thread_handle *handle, |
| 318 | int tracer_event_source_fd, |
| 319 | enum lttng_domain_type domain) |
| 320 | { |
| 321 | int ret; |
| 322 | enum lttng_error_code ret_code; |
| 323 | struct notification_thread_command cmd = {}; |
| 324 | |
| 325 | LTTNG_ASSERT(tracer_event_source_fd >= 0); |
| 326 | |
| 327 | init_notification_thread_command(&cmd); |
| 328 | |
| 329 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE; |
| 330 | cmd.parameters.tracer_event_source.tracer_event_source_fd = tracer_event_source_fd; |
| 331 | cmd.parameters.tracer_event_source.domain = domain; |
| 332 | |
| 333 | ret = run_command_wait(handle, &cmd); |
| 334 | if (ret) { |
| 335 | ret_code = LTTNG_ERR_UNK; |
| 336 | goto end; |
| 337 | } |
| 338 | |
| 339 | ret_code = cmd.reply_code; |
| 340 | end: |
| 341 | return ret_code; |
| 342 | } |
| 343 | |
| 344 | enum lttng_error_code |
| 345 | notification_thread_command_remove_tracer_event_source(struct notification_thread_handle *handle, |
| 346 | int tracer_event_source_fd) |
| 347 | { |
| 348 | int ret; |
| 349 | enum lttng_error_code ret_code; |
| 350 | struct notification_thread_command cmd = {}; |
| 351 | |
| 352 | init_notification_thread_command(&cmd); |
| 353 | |
| 354 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE; |
| 355 | cmd.parameters.tracer_event_source.tracer_event_source_fd = tracer_event_source_fd; |
| 356 | |
| 357 | ret = run_command_wait(handle, &cmd); |
| 358 | if (ret) { |
| 359 | ret_code = LTTNG_ERR_UNK; |
| 360 | goto end; |
| 361 | } |
| 362 | |
| 363 | ret_code = cmd.reply_code; |
| 364 | end: |
| 365 | return ret_code; |
| 366 | } |
| 367 | |
| 368 | enum lttng_error_code notification_thread_command_list_triggers( |
| 369 | struct notification_thread_handle *handle, uid_t uid, struct lttng_triggers **triggers) |
| 370 | { |
| 371 | int ret; |
| 372 | enum lttng_error_code ret_code; |
| 373 | struct notification_thread_command cmd = {}; |
| 374 | |
| 375 | LTTNG_ASSERT(handle); |
| 376 | LTTNG_ASSERT(triggers); |
| 377 | |
| 378 | init_notification_thread_command(&cmd); |
| 379 | |
| 380 | cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS; |
| 381 | cmd.parameters.list_triggers.uid = uid; |
| 382 | |
| 383 | ret = run_command_wait(handle, &cmd); |
| 384 | if (ret) { |
| 385 | ret_code = LTTNG_ERR_UNK; |
| 386 | goto end; |
| 387 | } |
| 388 | |
| 389 | ret_code = cmd.reply_code; |
| 390 | *triggers = cmd.reply.list_triggers.triggers; |
| 391 | |
| 392 | end: |
| 393 | return ret_code; |
| 394 | } |
| 395 | |
| 396 | void notification_thread_command_quit(struct notification_thread_handle *handle) |
| 397 | { |
| 398 | int ret; |
| 399 | struct notification_thread_command cmd = {}; |
| 400 | |
| 401 | init_notification_thread_command(&cmd); |
| 402 | |
| 403 | cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT; |
| 404 | ret = run_command_wait(handle, &cmd); |
| 405 | LTTNG_ASSERT(!ret && cmd.reply_code == LTTNG_OK); |
| 406 | } |
| 407 | |
| 408 | int notification_thread_client_communication_update( |
| 409 | struct notification_thread_handle *handle, |
| 410 | notification_client_id id, |
| 411 | enum client_transmission_status transmission_status) |
| 412 | { |
| 413 | struct notification_thread_command cmd = {}; |
| 414 | |
| 415 | init_notification_thread_command(&cmd); |
| 416 | |
| 417 | cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE; |
| 418 | cmd.parameters.client_communication_update.id = id; |
| 419 | cmd.parameters.client_communication_update.status = transmission_status; |
| 420 | return run_command_no_wait(handle, &cmd); |
| 421 | } |
| 422 | |
| 423 | enum lttng_error_code |
| 424 | notification_thread_command_get_trigger(struct notification_thread_handle *handle, |
| 425 | const struct lttng_trigger *trigger, |
| 426 | struct lttng_trigger **real_trigger) |
| 427 | { |
| 428 | int ret; |
| 429 | enum lttng_error_code ret_code; |
| 430 | struct notification_thread_command cmd = {}; |
| 431 | |
| 432 | init_notification_thread_command(&cmd); |
| 433 | |
| 434 | cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TRIGGER; |
| 435 | cmd.parameters.get_trigger.trigger = trigger; |
| 436 | ret = run_command_wait(handle, &cmd); |
| 437 | if (ret) { |
| 438 | ret_code = LTTNG_ERR_UNK; |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | ret_code = cmd.reply_code; |
| 443 | *real_trigger = cmd.reply.get_trigger.trigger; |
| 444 | |
| 445 | end: |
| 446 | return ret_code; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Takes ownership of the payload if present. |
| 451 | */ |
| 452 | struct lttng_event_notifier_notification *lttng_event_notifier_notification_create( |
| 453 | uint64_t tracer_token, enum lttng_domain_type domain, char *payload, size_t payload_size) |
| 454 | { |
| 455 | struct lttng_event_notifier_notification *notification = nullptr; |
| 456 | |
| 457 | LTTNG_ASSERT(domain != LTTNG_DOMAIN_NONE); |
| 458 | LTTNG_ASSERT((payload && payload_size) || (!payload && !payload_size)); |
| 459 | |
| 460 | notification = zmalloc<lttng_event_notifier_notification>(); |
| 461 | if (notification == nullptr) { |
| 462 | ERR("Error allocating notification"); |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | notification->tracer_token = tracer_token; |
| 467 | notification->type = domain; |
| 468 | notification->capture_buffer = payload; |
| 469 | notification->capture_buf_size = payload_size; |
| 470 | |
| 471 | end: |
| 472 | return notification; |
| 473 | } |
| 474 | |
| 475 | void lttng_event_notifier_notification_destroy( |
| 476 | struct lttng_event_notifier_notification *notification) |
| 477 | { |
| 478 | if (!notification) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | free(notification->capture_buffer); |
| 483 | free(notification); |
| 484 | } |