| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/error.h> |
| 9 | #include <common/mi-lttng.h> |
| 10 | #include <lttng/action/action-internal.h> |
| 11 | #include <lttng/action/list-internal.h> |
| 12 | #include <lttng/action/notify-internal.h> |
| 13 | #include <lttng/action/rate-policy-internal.h> |
| 14 | #include <lttng/action/rotate-session-internal.h> |
| 15 | #include <lttng/action/snapshot-session-internal.h> |
| 16 | #include <lttng/action/start-session-internal.h> |
| 17 | #include <lttng/action/stop-session-internal.h> |
| 18 | #include <lttng/error-query-internal.h> |
| 19 | |
| 20 | LTTNG_HIDDEN |
| 21 | const char *lttng_action_type_string(enum lttng_action_type action_type) |
| 22 | { |
| 23 | switch (action_type) { |
| 24 | case LTTNG_ACTION_TYPE_UNKNOWN: |
| 25 | return "UNKNOWN"; |
| 26 | case LTTNG_ACTION_TYPE_LIST: |
| 27 | return "LIST"; |
| 28 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 29 | return "NOTIFY"; |
| 30 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
| 31 | return "ROTATE_SESSION"; |
| 32 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
| 33 | return "SNAPSHOT_SESSION"; |
| 34 | case LTTNG_ACTION_TYPE_START_SESSION: |
| 35 | return "START_SESSION"; |
| 36 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
| 37 | return "STOP_SESSION"; |
| 38 | default: |
| 39 | return "???"; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | enum lttng_action_type lttng_action_get_type(const struct lttng_action *action) |
| 44 | { |
| 45 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; |
| 46 | } |
| 47 | |
| 48 | LTTNG_HIDDEN |
| 49 | void lttng_action_init(struct lttng_action *action, |
| 50 | enum lttng_action_type type, |
| 51 | action_validate_cb validate, |
| 52 | action_serialize_cb serialize, |
| 53 | action_equal_cb equal, |
| 54 | action_destroy_cb destroy, |
| 55 | action_get_rate_policy_cb get_rate_policy, |
| 56 | action_add_error_query_results_cb add_error_query_results, |
| 57 | action_mi_serialize_cb mi) |
| 58 | { |
| 59 | urcu_ref_init(&action->ref); |
| 60 | action->type = type; |
| 61 | action->validate = validate; |
| 62 | action->serialize = serialize; |
| 63 | action->equal = equal; |
| 64 | action->destroy = destroy; |
| 65 | action->get_rate_policy = get_rate_policy; |
| 66 | action->add_error_query_results = add_error_query_results; |
| 67 | action->mi_serialize = mi; |
| 68 | |
| 69 | action->execution_request_counter = 0; |
| 70 | action->execution_counter = 0; |
| 71 | action->execution_failure_counter = 0; |
| 72 | } |
| 73 | |
| 74 | static |
| 75 | void action_destroy_ref(struct urcu_ref *ref) |
| 76 | { |
| 77 | struct lttng_action *action = |
| 78 | container_of(ref, struct lttng_action, ref); |
| 79 | |
| 80 | action->destroy(action); |
| 81 | } |
| 82 | |
| 83 | LTTNG_HIDDEN |
| 84 | void lttng_action_get(struct lttng_action *action) |
| 85 | { |
| 86 | urcu_ref_get(&action->ref); |
| 87 | } |
| 88 | |
| 89 | LTTNG_HIDDEN |
| 90 | void lttng_action_put(struct lttng_action *action) |
| 91 | { |
| 92 | if (!action) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | LTTNG_ASSERT(action->destroy); |
| 97 | urcu_ref_put(&action->ref, action_destroy_ref); |
| 98 | } |
| 99 | |
| 100 | void lttng_action_destroy(struct lttng_action *action) |
| 101 | { |
| 102 | lttng_action_put(action); |
| 103 | } |
| 104 | |
| 105 | LTTNG_HIDDEN |
| 106 | bool lttng_action_validate(struct lttng_action *action) |
| 107 | { |
| 108 | bool valid; |
| 109 | |
| 110 | if (!action) { |
| 111 | valid = false; |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | if (!action->validate) { |
| 116 | /* Sub-class guarantees that it can never be invalid. */ |
| 117 | valid = true; |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | valid = action->validate(action); |
| 122 | end: |
| 123 | return valid; |
| 124 | } |
| 125 | |
| 126 | LTTNG_HIDDEN |
| 127 | int lttng_action_serialize(struct lttng_action *action, |
| 128 | struct lttng_payload *payload) |
| 129 | { |
| 130 | int ret; |
| 131 | struct lttng_action_comm action_comm = { |
| 132 | .action_type = (int8_t) action->type, |
| 133 | }; |
| 134 | |
| 135 | ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm, |
| 136 | sizeof(action_comm)); |
| 137 | if (ret) { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | ret = action->serialize(action, payload); |
| 142 | if (ret) { |
| 143 | goto end; |
| 144 | } |
| 145 | end: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | LTTNG_HIDDEN |
| 150 | ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view, |
| 151 | struct lttng_action **action) |
| 152 | { |
| 153 | ssize_t consumed_len, specific_action_consumed_len; |
| 154 | action_create_from_payload_cb create_from_payload_cb; |
| 155 | const struct lttng_action_comm *action_comm; |
| 156 | const struct lttng_payload_view action_comm_view = |
| 157 | lttng_payload_view_from_view( |
| 158 | view, 0, sizeof(*action_comm)); |
| 159 | |
| 160 | if (!view || !action) { |
| 161 | consumed_len = -1; |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | if (!lttng_payload_view_is_valid(&action_comm_view)) { |
| 166 | /* Payload not large enough to contain the header. */ |
| 167 | consumed_len = -1; |
| 168 | goto end; |
| 169 | } |
| 170 | |
| 171 | action_comm = (const struct lttng_action_comm *) action_comm_view.buffer.data; |
| 172 | |
| 173 | DBG("Create action from payload: action-type=%s", |
| 174 | lttng_action_type_string(action_comm->action_type)); |
| 175 | |
| 176 | switch (action_comm->action_type) { |
| 177 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 178 | create_from_payload_cb = lttng_action_notify_create_from_payload; |
| 179 | break; |
| 180 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
| 181 | create_from_payload_cb = |
| 182 | lttng_action_rotate_session_create_from_payload; |
| 183 | break; |
| 184 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
| 185 | create_from_payload_cb = |
| 186 | lttng_action_snapshot_session_create_from_payload; |
| 187 | break; |
| 188 | case LTTNG_ACTION_TYPE_START_SESSION: |
| 189 | create_from_payload_cb = |
| 190 | lttng_action_start_session_create_from_payload; |
| 191 | break; |
| 192 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
| 193 | create_from_payload_cb = |
| 194 | lttng_action_stop_session_create_from_payload; |
| 195 | break; |
| 196 | case LTTNG_ACTION_TYPE_LIST: |
| 197 | create_from_payload_cb = lttng_action_list_create_from_payload; |
| 198 | break; |
| 199 | default: |
| 200 | ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)", |
| 201 | action_comm->action_type, |
| 202 | lttng_action_type_string( |
| 203 | action_comm->action_type)); |
| 204 | consumed_len = -1; |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | { |
| 209 | /* Create buffer view for the action-type-specific data. */ |
| 210 | struct lttng_payload_view specific_action_view = |
| 211 | lttng_payload_view_from_view(view, |
| 212 | sizeof(struct lttng_action_comm), |
| 213 | -1); |
| 214 | |
| 215 | specific_action_consumed_len = create_from_payload_cb( |
| 216 | &specific_action_view, action); |
| 217 | } |
| 218 | if (specific_action_consumed_len < 0) { |
| 219 | ERR("Failed to create specific action from buffer."); |
| 220 | consumed_len = -1; |
| 221 | goto end; |
| 222 | } |
| 223 | |
| 224 | LTTNG_ASSERT(*action); |
| 225 | |
| 226 | consumed_len = sizeof(struct lttng_action_comm) + |
| 227 | specific_action_consumed_len; |
| 228 | |
| 229 | end: |
| 230 | return consumed_len; |
| 231 | } |
| 232 | |
| 233 | LTTNG_HIDDEN |
| 234 | bool lttng_action_is_equal(const struct lttng_action *a, |
| 235 | const struct lttng_action *b) |
| 236 | { |
| 237 | bool is_equal = false; |
| 238 | |
| 239 | if (!a || !b) { |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | if (a->type != b->type) { |
| 244 | goto end; |
| 245 | } |
| 246 | |
| 247 | if (a == b) { |
| 248 | is_equal = true; |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | LTTNG_ASSERT(a->equal); |
| 253 | is_equal = a->equal(a, b); |
| 254 | end: |
| 255 | return is_equal; |
| 256 | } |
| 257 | |
| 258 | LTTNG_HIDDEN |
| 259 | void lttng_action_increase_execution_request_count(struct lttng_action *action) |
| 260 | { |
| 261 | action->execution_request_counter++; |
| 262 | } |
| 263 | |
| 264 | LTTNG_HIDDEN |
| 265 | void lttng_action_increase_execution_count(struct lttng_action *action) |
| 266 | { |
| 267 | action->execution_counter++; |
| 268 | } |
| 269 | |
| 270 | LTTNG_HIDDEN |
| 271 | void lttng_action_increase_execution_failure_count(struct lttng_action *action) |
| 272 | { |
| 273 | uatomic_inc(&action->execution_failure_counter); |
| 274 | } |
| 275 | |
| 276 | LTTNG_HIDDEN |
| 277 | bool lttng_action_should_execute(const struct lttng_action *action) |
| 278 | { |
| 279 | const struct lttng_rate_policy *policy = NULL; |
| 280 | bool execute = false; |
| 281 | |
| 282 | if (action->get_rate_policy == NULL) { |
| 283 | execute = true; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | policy = action->get_rate_policy(action); |
| 288 | if (policy == NULL) { |
| 289 | execute = true; |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | execute = lttng_rate_policy_should_execute( |
| 294 | policy, action->execution_request_counter); |
| 295 | end: |
| 296 | return execute; |
| 297 | } |
| 298 | |
| 299 | LTTNG_HIDDEN |
| 300 | enum lttng_action_status lttng_action_add_error_query_results( |
| 301 | const struct lttng_action *action, |
| 302 | struct lttng_error_query_results *results) |
| 303 | { |
| 304 | return action->add_error_query_results(action, results); |
| 305 | } |
| 306 | |
| 307 | LTTNG_HIDDEN |
| 308 | enum lttng_action_status lttng_action_generic_add_error_query_results( |
| 309 | const struct lttng_action *action, |
| 310 | struct lttng_error_query_results *results) |
| 311 | { |
| 312 | enum lttng_action_status action_status; |
| 313 | struct lttng_error_query_result *error_counter = NULL; |
| 314 | const uint64_t execution_failure_counter = |
| 315 | uatomic_read(&action->execution_failure_counter); |
| 316 | |
| 317 | error_counter = lttng_error_query_result_counter_create( |
| 318 | "total execution failures", |
| 319 | "Aggregated count of errors encountered when executing the action", |
| 320 | execution_failure_counter); |
| 321 | if (!error_counter) { |
| 322 | action_status = LTTNG_ACTION_STATUS_ERROR; |
| 323 | goto end; |
| 324 | } |
| 325 | |
| 326 | if (lttng_error_query_results_add_result( |
| 327 | results, error_counter)) { |
| 328 | action_status = LTTNG_ACTION_STATUS_ERROR; |
| 329 | goto end; |
| 330 | } |
| 331 | |
| 332 | /* Ownership transferred to the results. */ |
| 333 | error_counter = NULL; |
| 334 | action_status = LTTNG_ACTION_STATUS_OK; |
| 335 | end: |
| 336 | lttng_error_query_result_destroy(error_counter); |
| 337 | return action_status; |
| 338 | } |
| 339 | |
| 340 | LTTNG_HIDDEN |
| 341 | enum lttng_error_code lttng_action_mi_serialize(const struct lttng_trigger *trigger, |
| 342 | const struct lttng_action *action, |
| 343 | struct mi_writer *writer, |
| 344 | const struct mi_lttng_error_query_callbacks |
| 345 | *error_query_callbacks, |
| 346 | struct lttng_dynamic_array *action_path_indexes) |
| 347 | { |
| 348 | int ret; |
| 349 | enum lttng_error_code ret_code; |
| 350 | struct lttng_action_path *action_path = NULL; |
| 351 | struct lttng_error_query_results *error_query_results = NULL; |
| 352 | |
| 353 | LTTNG_ASSERT(action); |
| 354 | LTTNG_ASSERT(writer); |
| 355 | |
| 356 | /* Open action. */ |
| 357 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_action); |
| 358 | if (ret) { |
| 359 | goto mi_error; |
| 360 | } |
| 361 | |
| 362 | if (action->type == LTTNG_ACTION_TYPE_LIST) { |
| 363 | /* |
| 364 | * Recursion is safe since action lists can't be nested for |
| 365 | * the moment. |
| 366 | */ |
| 367 | ret_code = lttng_action_list_mi_serialize(trigger, action, writer, |
| 368 | error_query_callbacks, action_path_indexes); |
| 369 | if (ret_code != LTTNG_OK) { |
| 370 | goto end; |
| 371 | } |
| 372 | |
| 373 | /* Nothing else to do. */ |
| 374 | goto close_action_element; |
| 375 | } |
| 376 | |
| 377 | LTTNG_ASSERT(action->mi_serialize); |
| 378 | ret_code = action->mi_serialize(action, writer); |
| 379 | if (ret_code != LTTNG_OK) { |
| 380 | goto end; |
| 381 | } |
| 382 | |
| 383 | /* Error query for the action. */ |
| 384 | if (error_query_callbacks && error_query_callbacks->action_cb) { |
| 385 | const uint64_t *action_path_indexes_raw_pointer = NULL; |
| 386 | const size_t action_path_indexes_size = |
| 387 | lttng_dynamic_array_get_count( |
| 388 | action_path_indexes); |
| 389 | |
| 390 | if (action_path_indexes_size != 0) { |
| 391 | action_path_indexes_raw_pointer = |
| 392 | (const uint64_t *) action_path_indexes |
| 393 | ->buffer.data; |
| 394 | } |
| 395 | |
| 396 | action_path = lttng_action_path_create( |
| 397 | action_path_indexes_raw_pointer, |
| 398 | action_path_indexes_size); |
| 399 | LTTNG_ASSERT(action_path); |
| 400 | |
| 401 | ret_code = error_query_callbacks->action_cb( |
| 402 | trigger, action_path, &error_query_results); |
| 403 | if (ret_code != LTTNG_OK) { |
| 404 | goto end; |
| 405 | } |
| 406 | |
| 407 | /* Serialize the error query results. */ |
| 408 | ret_code = lttng_error_query_results_mi_serialize( |
| 409 | error_query_results, writer); |
| 410 | if (ret_code != LTTNG_OK) { |
| 411 | goto end; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | close_action_element: |
| 416 | /* Close action. */ |
| 417 | ret = mi_lttng_writer_close_element(writer); |
| 418 | if (ret) { |
| 419 | goto mi_error; |
| 420 | } |
| 421 | |
| 422 | ret_code = LTTNG_OK; |
| 423 | goto end; |
| 424 | |
| 425 | mi_error: |
| 426 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 427 | end: |
| 428 | lttng_action_path_destroy(action_path); |
| 429 | lttng_error_query_results_destroy(error_query_results); |
| 430 | return ret_code; |
| 431 | } |