| 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 | lttng_condition_put(condition); |
| 28 | } |
| 29 | |
| 30 | static void condition_destroy_ref(struct urcu_ref *ref) |
| 31 | { |
| 32 | struct lttng_condition *condition = |
| 33 | container_of(ref, struct lttng_condition, ref); |
| 34 | |
| 35 | condition->destroy(condition); |
| 36 | } |
| 37 | |
| 38 | LTTNG_HIDDEN |
| 39 | void lttng_condition_get(struct lttng_condition *condition) |
| 40 | { |
| 41 | urcu_ref_get(&condition->ref); |
| 42 | } |
| 43 | |
| 44 | LTTNG_HIDDEN |
| 45 | void lttng_condition_put(struct lttng_condition *condition) |
| 46 | { |
| 47 | if (!condition) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | assert(condition->destroy); |
| 52 | urcu_ref_put(&condition->ref, condition_destroy_ref); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | LTTNG_HIDDEN |
| 57 | bool lttng_condition_validate(const struct lttng_condition *condition) |
| 58 | { |
| 59 | bool valid; |
| 60 | |
| 61 | if (!condition) { |
| 62 | valid = false; |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | if (!condition->validate) { |
| 67 | /* Sub-class guarantees that it can never be invalid. */ |
| 68 | valid = true; |
| 69 | goto end; |
| 70 | } |
| 71 | |
| 72 | valid = condition->validate(condition); |
| 73 | end: |
| 74 | return valid; |
| 75 | } |
| 76 | |
| 77 | LTTNG_HIDDEN |
| 78 | int lttng_condition_serialize(const struct lttng_condition *condition, |
| 79 | struct lttng_payload *payload) |
| 80 | { |
| 81 | int ret; |
| 82 | struct lttng_condition_comm condition_comm = {}; |
| 83 | |
| 84 | if (!condition) { |
| 85 | ret = -1; |
| 86 | goto end; |
| 87 | } |
| 88 | |
| 89 | condition_comm.condition_type = (int8_t) condition->type; |
| 90 | |
| 91 | ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm, |
| 92 | sizeof(condition_comm)); |
| 93 | if (ret) { |
| 94 | goto end; |
| 95 | } |
| 96 | |
| 97 | ret = condition->serialize(condition, payload); |
| 98 | if (ret) { |
| 99 | goto end; |
| 100 | } |
| 101 | end: |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | LTTNG_HIDDEN |
| 106 | bool lttng_condition_is_equal(const struct lttng_condition *a, |
| 107 | const struct lttng_condition *b) |
| 108 | { |
| 109 | bool is_equal = false; |
| 110 | |
| 111 | if (!a || !b) { |
| 112 | goto end; |
| 113 | } |
| 114 | |
| 115 | if (a->type != b->type) { |
| 116 | goto end; |
| 117 | } |
| 118 | |
| 119 | if (a == b) { |
| 120 | is_equal = true; |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | is_equal = a->equal ? a->equal(a, b) : true; |
| 125 | end: |
| 126 | return is_equal; |
| 127 | } |
| 128 | |
| 129 | LTTNG_HIDDEN |
| 130 | ssize_t lttng_condition_create_from_payload( |
| 131 | struct lttng_payload_view *view, |
| 132 | struct lttng_condition **condition) |
| 133 | { |
| 134 | ssize_t ret, condition_size = 0; |
| 135 | const struct lttng_condition_comm *condition_comm; |
| 136 | condition_create_from_payload_cb create_from_payload = NULL; |
| 137 | |
| 138 | if (!view || !condition) { |
| 139 | ret = -1; |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | DBG("Deserializing condition from buffer"); |
| 144 | condition_comm = (typeof(condition_comm)) view->buffer.data; |
| 145 | condition_size += sizeof(*condition_comm); |
| 146 | |
| 147 | switch ((enum lttng_condition_type) condition_comm->condition_type) { |
| 148 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 149 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
| 150 | break; |
| 151 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 152 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
| 153 | break; |
| 154 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 155 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
| 156 | break; |
| 157 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 158 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
| 159 | break; |
| 160 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 161 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
| 162 | break; |
| 163 | default: |
| 164 | ERR("Attempted to create condition of unknown type (%i)", |
| 165 | (int) condition_comm->condition_type); |
| 166 | ret = -1; |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | if (create_from_payload) { |
| 171 | struct lttng_payload_view condition_view = |
| 172 | lttng_payload_view_from_view(view, |
| 173 | sizeof(*condition_comm), -1); |
| 174 | |
| 175 | ret = create_from_payload(&condition_view, condition); |
| 176 | if (ret < 0) { |
| 177 | goto end; |
| 178 | } |
| 179 | condition_size += ret; |
| 180 | |
| 181 | } else { |
| 182 | abort(); |
| 183 | } |
| 184 | |
| 185 | ret = condition_size; |
| 186 | end: |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | LTTNG_HIDDEN |
| 191 | void lttng_condition_init(struct lttng_condition *condition, |
| 192 | enum lttng_condition_type type) |
| 193 | { |
| 194 | condition->type = type; |
| 195 | urcu_ref_init(&condition->ref); |
| 196 | } |