| 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 <common/macros.h> |
| 14 | #include <unistd.h> |
| 15 | #include <stdint.h> |
| 16 | #include <inttypes.h> |
| 17 | |
| 18 | static |
| 19 | void init_notification_thread_command(struct notification_thread_command *cmd) |
| 20 | { |
| 21 | CDS_INIT_LIST_HEAD(&cmd->cmd_list_node); |
| 22 | lttng_waiter_init(&cmd->reply_waiter); |
| 23 | } |
| 24 | |
| 25 | static |
| 26 | 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, |
| 35 | &handle->cmd_queue.list); |
| 36 | /* Wake-up thread. */ |
| 37 | ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe), |
| 38 | ¬ification_counter, sizeof(notification_counter)); |
| 39 | if (ret != sizeof(notification_counter)) { |
| 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 | |
| 50 | lttng_waiter_wait(&cmd->reply_waiter); |
| 51 | return 0; |
| 52 | error_unlock_queue: |
| 53 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | static |
| 58 | struct 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); |
| 70 | end: |
| 71 | return new_cmd; |
| 72 | } |
| 73 | |
| 74 | static |
| 75 | int 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 | ¬ification_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; |
| 106 | error_unlock_queue: |
| 107 | free(new_cmd); |
| 108 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 109 | error: |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | enum 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; |
| 119 | struct notification_thread_command cmd = {}; |
| 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; |
| 132 | end: |
| 133 | return ret_code; |
| 134 | } |
| 135 | |
| 136 | enum 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; |
| 142 | struct notification_thread_command cmd = {}; |
| 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; |
| 155 | end: |
| 156 | return ret_code; |
| 157 | } |
| 158 | |
| 159 | enum 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; |
| 167 | struct notification_thread_command cmd = {}; |
| 168 | |
| 169 | init_notification_thread_command(&cmd); |
| 170 | |
| 171 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL; |
| 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; |
| 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; |
| 186 | end: |
| 187 | return ret_code; |
| 188 | } |
| 189 | |
| 190 | enum 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; |
| 196 | struct notification_thread_command cmd = {}; |
| 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; |
| 210 | end: |
| 211 | return ret_code; |
| 212 | } |
| 213 | |
| 214 | enum 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; |
| 221 | struct notification_thread_command cmd = {}; |
| 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; |
| 238 | end: |
| 239 | return ret_code; |
| 240 | } |
| 241 | |
| 242 | enum 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; |
| 250 | struct notification_thread_command cmd = {}; |
| 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; |
| 268 | end: |
| 269 | return ret_code; |
| 270 | } |
| 271 | |
| 272 | enum 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; |
| 291 | end: |
| 292 | return ret_code; |
| 293 | } |
| 294 | |
| 295 | enum 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; |
| 314 | end: |
| 315 | return ret_code; |
| 316 | } |
| 317 | |
| 318 | enum 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 | |
| 341 | end: |
| 342 | return ret_code; |
| 343 | } |
| 344 | |
| 345 | enum 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 | |
| 372 | end: |
| 373 | return ret_code; |
| 374 | } |
| 375 | |
| 376 | void notification_thread_command_quit( |
| 377 | struct notification_thread_handle *handle) |
| 378 | { |
| 379 | int ret; |
| 380 | struct notification_thread_command cmd = {}; |
| 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 | } |
| 388 | |
| 389 | int 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 | */ |
| 407 | LTTNG_HIDDEN |
| 408 | struct 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 | |
| 435 | end: |
| 436 | return notification; |
| 437 | } |
| 438 | |
| 439 | LTTNG_HIDDEN |
| 440 | void 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 | } |