| 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 <lttng/condition/condition-internal.h> |
| 9 | #include <lttng/condition/buffer-usage-internal.h> |
| 10 | #include <lttng/condition/session-consumed-size-internal.h> |
| 11 | #include <lttng/condition/session-rotation-internal.h> |
| 12 | #include <common/macros.h> |
| 13 | #include <common/error.h> |
| 14 | #include <common/dynamic-buffer.h> |
| 15 | #include <common/buffer-view.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <assert.h> |
| 18 | |
| 19 | enum lttng_condition_type lttng_condition_get_type( |
| 20 | const struct lttng_condition *condition) |
| 21 | { |
| 22 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 23 | } |
| 24 | |
| 25 | void lttng_condition_destroy(struct lttng_condition *condition) |
| 26 | { |
| 27 | if (!condition) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | assert(condition->destroy); |
| 32 | condition->destroy(condition); |
| 33 | } |
| 34 | |
| 35 | LTTNG_HIDDEN |
| 36 | bool lttng_condition_validate(const struct lttng_condition *condition) |
| 37 | { |
| 38 | bool valid; |
| 39 | |
| 40 | if (!condition) { |
| 41 | valid = false; |
| 42 | goto end; |
| 43 | } |
| 44 | |
| 45 | if (!condition->validate) { |
| 46 | /* Sub-class guarantees that it can never be invalid. */ |
| 47 | valid = true; |
| 48 | goto end; |
| 49 | } |
| 50 | |
| 51 | valid = condition->validate(condition); |
| 52 | end: |
| 53 | return valid; |
| 54 | } |
| 55 | |
| 56 | LTTNG_HIDDEN |
| 57 | int lttng_condition_serialize(const struct lttng_condition *condition, |
| 58 | struct lttng_dynamic_buffer *buf) |
| 59 | { |
| 60 | int ret; |
| 61 | struct lttng_condition_comm condition_comm = { 0 }; |
| 62 | |
| 63 | if (!condition) { |
| 64 | ret = -1; |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | condition_comm.condition_type = (int8_t) condition->type; |
| 69 | |
| 70 | ret = lttng_dynamic_buffer_append(buf, &condition_comm, |
| 71 | sizeof(condition_comm)); |
| 72 | if (ret) { |
| 73 | goto end; |
| 74 | } |
| 75 | |
| 76 | ret = condition->serialize(condition, buf); |
| 77 | if (ret) { |
| 78 | goto end; |
| 79 | } |
| 80 | end: |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | LTTNG_HIDDEN |
| 85 | bool lttng_condition_is_equal(const struct lttng_condition *a, |
| 86 | const struct lttng_condition *b) |
| 87 | { |
| 88 | bool is_equal = false; |
| 89 | |
| 90 | if (!a || !b) { |
| 91 | goto end; |
| 92 | } |
| 93 | |
| 94 | if (a->type != b->type) { |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | if (a == b) { |
| 99 | is_equal = true; |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | is_equal = a->equal ? a->equal(a, b) : true; |
| 104 | end: |
| 105 | return is_equal; |
| 106 | } |
| 107 | |
| 108 | LTTNG_HIDDEN |
| 109 | ssize_t lttng_condition_create_from_buffer( |
| 110 | const struct lttng_buffer_view *buffer, |
| 111 | struct lttng_condition **condition) |
| 112 | { |
| 113 | ssize_t ret, condition_size = 0; |
| 114 | const struct lttng_condition_comm *condition_comm; |
| 115 | condition_create_from_buffer_cb create_from_buffer = NULL; |
| 116 | |
| 117 | if (!buffer || !condition) { |
| 118 | ret = -1; |
| 119 | goto end; |
| 120 | } |
| 121 | |
| 122 | DBG("Deserializing condition from buffer"); |
| 123 | condition_comm = (const struct lttng_condition_comm *) buffer->data; |
| 124 | condition_size += sizeof(*condition_comm); |
| 125 | |
| 126 | switch ((enum lttng_condition_type) condition_comm->condition_type) { |
| 127 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 128 | create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer; |
| 129 | break; |
| 130 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 131 | create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer; |
| 132 | break; |
| 133 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 134 | create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer; |
| 135 | break; |
| 136 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 137 | create_from_buffer = lttng_condition_session_rotation_ongoing_create_from_buffer; |
| 138 | break; |
| 139 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 140 | create_from_buffer = lttng_condition_session_rotation_completed_create_from_buffer; |
| 141 | break; |
| 142 | default: |
| 143 | ERR("Attempted to create condition of unknown type (%i)", |
| 144 | (int) condition_comm->condition_type); |
| 145 | ret = -1; |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | if (create_from_buffer) { |
| 150 | const struct lttng_buffer_view view = |
| 151 | lttng_buffer_view_from_view(buffer, |
| 152 | sizeof(*condition_comm), -1); |
| 153 | |
| 154 | ret = create_from_buffer(&view, condition); |
| 155 | if (ret < 0) { |
| 156 | goto end; |
| 157 | } |
| 158 | condition_size += ret; |
| 159 | |
| 160 | } else { |
| 161 | abort(); |
| 162 | } |
| 163 | |
| 164 | ret = condition_size; |
| 165 | end: |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | LTTNG_HIDDEN |
| 170 | void lttng_condition_init(struct lttng_condition *condition, |
| 171 | enum lttng_condition_type type) |
| 172 | { |
| 173 | condition->type = type; |
| 174 | } |