| 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-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 | if (!condition) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | assert(condition->destroy); |
| 33 | condition->destroy(condition); |
| 34 | } |
| 35 | |
| 36 | LTTNG_HIDDEN |
| 37 | bool lttng_condition_validate(const struct lttng_condition *condition) |
| 38 | { |
| 39 | bool valid; |
| 40 | |
| 41 | if (!condition) { |
| 42 | valid = false; |
| 43 | goto end; |
| 44 | } |
| 45 | |
| 46 | if (!condition->validate) { |
| 47 | /* Sub-class guarantees that it can never be invalid. */ |
| 48 | valid = true; |
| 49 | goto end; |
| 50 | } |
| 51 | |
| 52 | valid = condition->validate(condition); |
| 53 | end: |
| 54 | return valid; |
| 55 | } |
| 56 | |
| 57 | LTTNG_HIDDEN |
| 58 | int lttng_condition_serialize(const struct lttng_condition *condition, |
| 59 | struct lttng_dynamic_buffer *buf, |
| 60 | int *fd_to_send) |
| 61 | { |
| 62 | int ret; |
| 63 | struct lttng_condition_comm condition_comm = { 0 }; |
| 64 | |
| 65 | if (!condition) { |
| 66 | ret = -1; |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | condition_comm.condition_type = (int8_t) condition->type; |
| 71 | |
| 72 | ret = lttng_dynamic_buffer_append(buf, &condition_comm, |
| 73 | sizeof(condition_comm)); |
| 74 | if (ret) { |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | ret = condition->serialize(condition, buf, fd_to_send); |
| 79 | if (ret) { |
| 80 | goto end; |
| 81 | } |
| 82 | end: |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | LTTNG_HIDDEN |
| 87 | bool lttng_condition_is_equal(const struct lttng_condition *a, |
| 88 | const struct lttng_condition *b) |
| 89 | { |
| 90 | bool is_equal = false; |
| 91 | |
| 92 | if (!a || !b) { |
| 93 | goto end; |
| 94 | } |
| 95 | |
| 96 | if (a->type != b->type) { |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | if (a == b) { |
| 101 | is_equal = true; |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | is_equal = a->equal ? a->equal(a, b) : true; |
| 106 | end: |
| 107 | return is_equal; |
| 108 | } |
| 109 | |
| 110 | LTTNG_HIDDEN |
| 111 | ssize_t lttng_condition_create_from_buffer( |
| 112 | const struct lttng_buffer_view *buffer, |
| 113 | struct lttng_condition **condition) |
| 114 | { |
| 115 | ssize_t ret, condition_size = 0; |
| 116 | const struct lttng_condition_comm *condition_comm; |
| 117 | condition_create_from_buffer_cb create_from_buffer = NULL; |
| 118 | |
| 119 | if (!buffer || !condition) { |
| 120 | ret = -1; |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | DBG("Deserializing condition from buffer"); |
| 125 | condition_comm = (const struct lttng_condition_comm *) buffer->data; |
| 126 | condition_size += sizeof(*condition_comm); |
| 127 | |
| 128 | switch ((enum lttng_condition_type) condition_comm->condition_type) { |
| 129 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 130 | create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer; |
| 131 | break; |
| 132 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 133 | create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer; |
| 134 | break; |
| 135 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 136 | create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer; |
| 137 | break; |
| 138 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 139 | create_from_buffer = lttng_condition_session_rotation_ongoing_create_from_buffer; |
| 140 | break; |
| 141 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 142 | create_from_buffer = lttng_condition_session_rotation_completed_create_from_buffer; |
| 143 | break; |
| 144 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: |
| 145 | create_from_buffer = lttng_condition_event_rule_create_from_buffer; |
| 146 | break; |
| 147 | default: |
| 148 | ERR("Attempted to create condition of unknown type (%i)", |
| 149 | (int) condition_comm->condition_type); |
| 150 | ret = -1; |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | if (create_from_buffer) { |
| 155 | const struct lttng_buffer_view view = |
| 156 | lttng_buffer_view_from_view(buffer, |
| 157 | sizeof(*condition_comm), -1); |
| 158 | |
| 159 | ret = create_from_buffer(&view, condition); |
| 160 | if (ret < 0) { |
| 161 | goto end; |
| 162 | } |
| 163 | condition_size += ret; |
| 164 | |
| 165 | } else { |
| 166 | abort(); |
| 167 | } |
| 168 | |
| 169 | ret = condition_size; |
| 170 | end: |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | LTTNG_HIDDEN |
| 175 | void lttng_condition_init(struct lttng_condition *condition, |
| 176 | enum lttng_condition_type type) |
| 177 | { |
| 178 | condition->type = type; |
| 179 | } |
| 180 | |
| 181 | LTTNG_HIDDEN |
| 182 | const char *lttng_condition_type_str(enum lttng_condition_type type) |
| 183 | { |
| 184 | switch (type) { |
| 185 | case LTTNG_CONDITION_TYPE_UNKNOWN: |
| 186 | return "unknown"; |
| 187 | |
| 188 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 189 | return "session consumed size"; |
| 190 | |
| 191 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 192 | return "buffer usage high"; |
| 193 | |
| 194 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 195 | return "buffer usage low"; |
| 196 | |
| 197 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
| 198 | return "session rotation ongoing"; |
| 199 | |
| 200 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: |
| 201 | return "session rotation completed"; |
| 202 | |
| 203 | case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: |
| 204 | return "event rule hit"; |
| 205 | |
| 206 | default: |
| 207 | return "???"; |
| 208 | } |
| 209 | } |