| 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 | ¬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 |
| 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 | ¬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 | |
| 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 | init_notification_thread_command(&cmd); |
| 121 | |
| 122 | cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER; |
| 123 | cmd.parameters.trigger = trigger; |
| 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 notification_thread_command_unregister_trigger( |
| 136 | struct notification_thread_handle *handle, |
| 137 | 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.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 notification_thread_command_add_channel( |
| 159 | struct notification_thread_handle *handle, |
| 160 | char *session_name, uid_t uid, gid_t gid, |
| 161 | char *channel_name, uint64_t key, |
| 162 | enum lttng_domain_type domain, uint64_t capacity) |
| 163 | { |
| 164 | int ret; |
| 165 | enum lttng_error_code ret_code; |
| 166 | struct notification_thread_command cmd = {}; |
| 167 | |
| 168 | init_notification_thread_command(&cmd); |
| 169 | |
| 170 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL; |
| 171 | cmd.parameters.add_channel.session.name = session_name; |
| 172 | cmd.parameters.add_channel.session.uid = uid; |
| 173 | cmd.parameters.add_channel.session.gid = gid; |
| 174 | cmd.parameters.add_channel.channel.name = channel_name; |
| 175 | cmd.parameters.add_channel.channel.key = key; |
| 176 | cmd.parameters.add_channel.channel.domain = domain; |
| 177 | cmd.parameters.add_channel.channel.capacity = capacity; |
| 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_channel( |
| 190 | struct notification_thread_handle *handle, |
| 191 | uint64_t key, enum lttng_domain_type domain) |
| 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_CHANNEL; |
| 200 | cmd.parameters.remove_channel.key = key; |
| 201 | cmd.parameters.remove_channel.domain = domain; |
| 202 | |
| 203 | ret = run_command_wait(handle, &cmd); |
| 204 | if (ret) { |
| 205 | ret_code = LTTNG_ERR_UNK; |
| 206 | goto end; |
| 207 | } |
| 208 | ret_code = cmd.reply_code; |
| 209 | end: |
| 210 | return ret_code; |
| 211 | } |
| 212 | |
| 213 | enum lttng_error_code notification_thread_command_session_rotation_ongoing( |
| 214 | struct notification_thread_handle *handle, |
| 215 | const char *session_name, uid_t uid, gid_t gid, |
| 216 | uint64_t trace_archive_chunk_id) |
| 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_SESSION_ROTATION_ONGOING; |
| 225 | cmd.parameters.session_rotation.session_name = session_name; |
| 226 | cmd.parameters.session_rotation.uid = uid; |
| 227 | cmd.parameters.session_rotation.gid = gid; |
| 228 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
| 229 | trace_archive_chunk_id; |
| 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_session_rotation_completed( |
| 242 | struct notification_thread_handle *handle, |
| 243 | const char *session_name, uid_t uid, gid_t gid, |
| 244 | uint64_t trace_archive_chunk_id, |
| 245 | struct lttng_trace_archive_location *location) |
| 246 | { |
| 247 | int ret; |
| 248 | enum lttng_error_code ret_code; |
| 249 | struct notification_thread_command cmd = {}; |
| 250 | |
| 251 | init_notification_thread_command(&cmd); |
| 252 | |
| 253 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED; |
| 254 | cmd.parameters.session_rotation.session_name = session_name; |
| 255 | cmd.parameters.session_rotation.uid = uid; |
| 256 | cmd.parameters.session_rotation.gid = gid; |
| 257 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
| 258 | trace_archive_chunk_id; |
| 259 | cmd.parameters.session_rotation.location = location; |
| 260 | |
| 261 | ret = run_command_wait(handle, &cmd); |
| 262 | if (ret) { |
| 263 | ret_code = LTTNG_ERR_UNK; |
| 264 | goto end; |
| 265 | } |
| 266 | ret_code = cmd.reply_code; |
| 267 | end: |
| 268 | return ret_code; |
| 269 | } |
| 270 | |
| 271 | enum lttng_error_code notification_thread_command_add_application( |
| 272 | struct notification_thread_handle *handle, |
| 273 | int fd, |
| 274 | enum lttng_domain_type domain) |
| 275 | { |
| 276 | int ret; |
| 277 | enum lttng_error_code ret_code; |
| 278 | struct notification_thread_command cmd = {}; |
| 279 | |
| 280 | assert(!(fd < 0)); |
| 281 | |
| 282 | init_notification_thread_command(&cmd); |
| 283 | |
| 284 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION; |
| 285 | cmd.parameters.application.read_side_trigger_event_application_pipe = fd; |
| 286 | cmd.parameters.application.domain = domain; |
| 287 | |
| 288 | ret = run_command_wait(handle, &cmd); |
| 289 | if (ret) { |
| 290 | ret_code = LTTNG_ERR_UNK; |
| 291 | goto end; |
| 292 | } |
| 293 | ret_code = cmd.reply_code; |
| 294 | end: |
| 295 | return ret_code; |
| 296 | } |
| 297 | |
| 298 | enum lttng_error_code notification_thread_command_remove_application( |
| 299 | struct notification_thread_handle *handle, |
| 300 | int fd) |
| 301 | { |
| 302 | int ret; |
| 303 | enum lttng_error_code ret_code; |
| 304 | struct notification_thread_command cmd = {}; |
| 305 | |
| 306 | init_notification_thread_command(&cmd); |
| 307 | |
| 308 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION; |
| 309 | cmd.parameters.application.read_side_trigger_event_application_pipe = fd; |
| 310 | |
| 311 | ret = run_command_wait(handle, &cmd); |
| 312 | if (ret) { |
| 313 | ret_code = LTTNG_ERR_UNK; |
| 314 | goto end; |
| 315 | } |
| 316 | ret_code = cmd.reply_code; |
| 317 | end: |
| 318 | return ret_code; |
| 319 | } |
| 320 | |
| 321 | enum lttng_error_code notification_thread_command_get_tokens( |
| 322 | struct notification_thread_handle *handle, |
| 323 | struct lttng_triggers **tokens_triggers) |
| 324 | { |
| 325 | int ret; |
| 326 | enum lttng_error_code ret_code; |
| 327 | struct notification_thread_command cmd = {}; |
| 328 | |
| 329 | assert(handle); |
| 330 | assert(tokens_triggers); |
| 331 | |
| 332 | init_notification_thread_command(&cmd); |
| 333 | |
| 334 | cmd.type = NOTIFICATION_COMMAND_TYPE_GET_TOKENS; |
| 335 | |
| 336 | ret = run_command_wait(handle, &cmd); |
| 337 | if (ret) { |
| 338 | ret_code = LTTNG_ERR_UNK; |
| 339 | goto end; |
| 340 | } |
| 341 | ret_code = cmd.reply_code; |
| 342 | *tokens_triggers = cmd.reply.get_tokens.triggers; |
| 343 | |
| 344 | end: |
| 345 | return ret_code; |
| 346 | } |
| 347 | |
| 348 | enum lttng_error_code notification_thread_command_list_triggers( |
| 349 | struct notification_thread_handle *handle, |
| 350 | uid_t uid, |
| 351 | struct lttng_triggers **triggers) |
| 352 | { |
| 353 | int ret; |
| 354 | enum lttng_error_code ret_code; |
| 355 | struct notification_thread_command cmd = {}; |
| 356 | |
| 357 | assert(handle); |
| 358 | assert(triggers); |
| 359 | |
| 360 | init_notification_thread_command(&cmd); |
| 361 | |
| 362 | cmd.type = NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS; |
| 363 | cmd.parameters.list_triggers.uid = uid; |
| 364 | |
| 365 | ret = run_command_wait(handle, &cmd); |
| 366 | if (ret) { |
| 367 | ret_code = LTTNG_ERR_UNK; |
| 368 | goto end; |
| 369 | } |
| 370 | ret_code = cmd.reply_code; |
| 371 | *triggers = cmd.reply.list_triggers.triggers; |
| 372 | |
| 373 | end: |
| 374 | return ret_code; |
| 375 | } |
| 376 | |
| 377 | void notification_thread_command_quit( |
| 378 | struct notification_thread_handle *handle) |
| 379 | { |
| 380 | int ret; |
| 381 | struct notification_thread_command cmd = {}; |
| 382 | |
| 383 | init_notification_thread_command(&cmd); |
| 384 | |
| 385 | cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT; |
| 386 | ret = run_command_wait(handle, &cmd); |
| 387 | assert(!ret && cmd.reply_code == LTTNG_OK); |
| 388 | } |
| 389 | |
| 390 | int notification_thread_client_communication_update( |
| 391 | struct notification_thread_handle *handle, |
| 392 | notification_client_id id, |
| 393 | enum client_transmission_status transmission_status) |
| 394 | { |
| 395 | struct notification_thread_command cmd = {}; |
| 396 | |
| 397 | init_notification_thread_command(&cmd); |
| 398 | |
| 399 | cmd.type = NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE; |
| 400 | cmd.parameters.client_communication_update.id = id; |
| 401 | cmd.parameters.client_communication_update.status = transmission_status; |
| 402 | return run_command_no_wait(handle, &cmd); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Takes ownership of the payload if present. |
| 407 | */ |
| 408 | LTTNG_HIDDEN |
| 409 | struct lttng_trigger_notification *lttng_trigger_notification_create( |
| 410 | uint64_t id, |
| 411 | enum lttng_domain_type domain, |
| 412 | char *payload, |
| 413 | size_t payload_size) |
| 414 | { |
| 415 | struct lttng_trigger_notification *notification = NULL; |
| 416 | |
| 417 | assert(domain != LTTNG_DOMAIN_NONE); |
| 418 | |
| 419 | if (payload) { |
| 420 | assert(payload_size > 0); |
| 421 | } else { |
| 422 | assert(payload_size == 0); |
| 423 | } |
| 424 | |
| 425 | notification = zmalloc(sizeof(struct lttng_trigger_notification)); |
| 426 | if (notification == NULL) { |
| 427 | ERR("[notification-thread] Error allocating notification "); |
| 428 | goto end; |
| 429 | } |
| 430 | |
| 431 | notification->id = id; |
| 432 | notification->type = domain; |
| 433 | notification->capture_buffer = payload; |
| 434 | notification->capture_buf_size = payload_size; |
| 435 | |
| 436 | end: |
| 437 | return notification; |
| 438 | } |
| 439 | |
| 440 | LTTNG_HIDDEN |
| 441 | void lttng_trigger_notification_destroy( |
| 442 | struct lttng_trigger_notification *notification) |
| 443 | { |
| 444 | if (!notification) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | if(notification->capture_buffer) { |
| 449 | free(notification->capture_buffer); |
| 450 | } |
| 451 | free(notification); |
| 452 | } |