| 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/event-rule-matches-internal.h> |
| 11 | #include <lttng/condition/session-consumed-size-internal.h> |
| 12 | #include <lttng/condition/session-rotation-internal.h> |
| 13 | #include <common/macros.h> |
| 14 | #include <common/error.h> |
| 15 | #include <common/dynamic-buffer.h> |
| 16 | #include <common/buffer-view.h> |
| 17 | #include <stdbool.h> |
| 18 | #include <assert.h> |
| 19 | |
| 20 | enum lttng_condition_type lttng_condition_get_type( |
| 21 | const struct lttng_condition *condition) |
| 22 | { |
| 23 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 24 | } |
| 25 | |
| 26 | void lttng_condition_destroy(struct lttng_condition *condition) |
| 27 | { |
| 28 | lttng_condition_put(condition); |
| 29 | } |
| 30 | |
| 31 | static void condition_destroy_ref(struct urcu_ref *ref) |
| 32 | { |
| 33 | struct lttng_condition *condition = |
| 34 | container_of(ref, struct lttng_condition, ref); |
| 35 | |
| 36 | condition->destroy(condition); |
| 37 | } |
| 38 | |
| 39 | LTTNG_HIDDEN |
| 40 | void lttng_condition_get(struct lttng_condition *condition) |
| 41 | { |
| 42 | urcu_ref_get(&condition->ref); |
| 43 | } |
| 44 | |
| 45 | LTTNG_HIDDEN |
| 46 | void lttng_condition_put(struct lttng_condition *condition) |
| 47 | { |
| 48 | if (!condition) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | assert(condition->destroy); |
| 53 | urcu_ref_put(&condition->ref, condition_destroy_ref); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | LTTNG_HIDDEN |
| 58 | bool lttng_condition_validate(const struct lttng_condition *condition) |
| 59 | { |
| 60 | bool valid; |
| 61 | |
| 62 | if (!condition) { |
| 63 | valid = false; |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | if (!condition->validate) { |
| 68 | /* Sub-class guarantees that it can never be invalid. */ |
| 69 | valid = true; |
| 70 | goto end; |
| 71 | } |
| 72 | |
| 73 | valid = condition->validate(condition); |
| 74 | end: |
| 75 | return valid; |
| 76 | } |
| 77 | |
| 78 | LTTNG_HIDDEN |
| 79 | int lttng_condition_serialize(const struct lttng_condition *condition, |
| 80 | struct lttng_payload *payload) |
| 81 | { |
| 82 | int ret; |
| 83 | struct lttng_condition_comm condition_comm = {}; |
| 84 | |
| 85 | if (!condition) { |
| 86 | ret = -1; |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | condition_comm.condition_type = (int8_t) condition->type; |
| 91 | |
| 92 | ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm, |
| 93 | sizeof(condition_comm)); |
| 94 | if (ret) { |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | ret = condition->serialize(condition, payload); |
| 99 | if (ret) { |
| 100 | goto end; |
| 101 | } |
| 102 | end: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | LTTNG_HIDDEN |
| 107 | bool lttng_condition_is_equal(const struct lttng_condition *a, |
| 108 | const struct lttng_condition *b) |
| 109 | { |
| 110 | bool is_equal = false; |
| 111 | |
| 112 | if (!a || !b) { |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | if (a->type != b->type) { |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | if (a == b) { |
| 121 | is_equal = true; |
| 122 | goto end; |
| 123 | } |
| 124 | |
| 125 | is_equal = a->equal ? a->equal(a, b) : true; |
| 126 | end: |
| 127 | return is_equal; |
| 128 | } |
| 129 | |
| 130 | LTTNG_HIDDEN |
| 131 | ssize_t lttng_condition_create_from_payload( |
| 132 | struct lttng_payload_view *view, |
| 133 | struct lttng_condition **condition) |
| 134 | { |
| 135 | ssize_t ret, condition_size = 0; |
| 136 | condition_create_from_payload_cb create_from_payload = NULL; |
| 137 | const struct lttng_condition_comm *condition_comm; |
| 138 | const struct lttng_payload_view condition_comm_view = |
| 139 | lttng_payload_view_from_view( |
| 140 | view, 0, sizeof(*condition_comm)); |
| 141 | |
| 142 | if (!view || !condition) { |
| 143 | ret = -1; |
| 144 | goto end; |
| 145 | } |
| 146 | |
| 147 | if (!lttng_payload_view_is_valid(&condition_comm_view)) { |
| 148 | /* Payload not large enough to contain the header. */ |
| 149 | ret = -1; |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | DBG("Deserializing condition from buffer"); |
| 154 | condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data; |
| 155 | condition_size += sizeof(*condition_comm); |
| 156 | |
| 157 | switch ((enum lttng_condition_type) condition_comm->condition_type) { |
| 158 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 159 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
| 160 | break; |
| 161 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 162 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
| 163 | break; |
| 164 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 165 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
| 166 | break; |
| 167 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 168 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
| 169 | break; |
| 170 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 171 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
| 172 | break; |
| 173 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 174 | create_from_payload = |
| 175 | lttng_condition_event_rule_matches_create_from_payload; |
| 176 | break; |
| 177 | default: |
| 178 | ERR("Attempted to create condition of unknown type (%i)", |
| 179 | (int) condition_comm->condition_type); |
| 180 | ret = -1; |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | if (create_from_payload) { |
| 185 | struct lttng_payload_view condition_view = |
| 186 | lttng_payload_view_from_view(view, |
| 187 | sizeof(*condition_comm), -1); |
| 188 | |
| 189 | ret = create_from_payload(&condition_view, condition); |
| 190 | if (ret < 0) { |
| 191 | goto end; |
| 192 | } |
| 193 | condition_size += ret; |
| 194 | |
| 195 | } else { |
| 196 | abort(); |
| 197 | } |
| 198 | |
| 199 | ret = condition_size; |
| 200 | end: |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | LTTNG_HIDDEN |
| 205 | void lttng_condition_init(struct lttng_condition *condition, |
| 206 | enum lttng_condition_type type) |
| 207 | { |
| 208 | condition->type = type; |
| 209 | urcu_ref_init(&condition->ref); |
| 210 | } |
| 211 | |
| 212 | LTTNG_HIDDEN |
| 213 | const char *lttng_condition_type_str(enum lttng_condition_type type) |
| 214 | { |
| 215 | switch (type) { |
| 216 | case LTTNG_CONDITION_TYPE_UNKNOWN: |
| 217 | return "unknown"; |
| 218 | |
| 219 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 220 | return "session consumed size"; |
| 221 | |
| 222 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 223 | return "buffer usage high"; |
| 224 | |
| 225 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 226 | return "buffer usage low"; |
| 227 | |
| 228 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 229 | return "session rotation ongoing"; |
| 230 | |
| 231 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 232 | return "session rotation completed"; |
| 233 | |
| 234 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
| 235 | return "event rule matches"; |
| 236 | |
| 237 | default: |
| 238 | return "???"; |
| 239 | } |
| 240 | } |