| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <lttng/condition/condition-internal.h> |
| 19 | #include <lttng/condition/buffer-usage-internal.h> |
| 20 | #include <lttng/condition/session-consumed-size-internal.h> |
| 21 | #include <common/macros.h> |
| 22 | #include <common/error.h> |
| 23 | #include <common/dynamic-buffer.h> |
| 24 | #include <common/buffer-view.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | enum lttng_condition_type lttng_condition_get_type( |
| 29 | const struct lttng_condition *condition) |
| 30 | { |
| 31 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 32 | } |
| 33 | |
| 34 | void lttng_condition_destroy(struct lttng_condition *condition) |
| 35 | { |
| 36 | if (!condition) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | assert(condition->destroy); |
| 41 | condition->destroy(condition); |
| 42 | } |
| 43 | |
| 44 | LTTNG_HIDDEN |
| 45 | bool lttng_condition_validate(const struct lttng_condition *condition) |
| 46 | { |
| 47 | bool valid; |
| 48 | |
| 49 | if (!condition) { |
| 50 | valid = false; |
| 51 | goto end; |
| 52 | } |
| 53 | |
| 54 | if (!condition->validate) { |
| 55 | /* Sub-class guarantees that it can never be invalid. */ |
| 56 | valid = true; |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | valid = condition->validate(condition); |
| 61 | end: |
| 62 | return valid; |
| 63 | } |
| 64 | |
| 65 | LTTNG_HIDDEN |
| 66 | ssize_t lttng_condition_serialize(const struct lttng_condition *condition, |
| 67 | char *buf) |
| 68 | { |
| 69 | ssize_t ret, condition_size; |
| 70 | struct lttng_condition_comm condition_comm = { |
| 71 | .condition_type = (int8_t) (condition ? |
| 72 | condition->type : LTTNG_CONDITION_TYPE_UNKNOWN) |
| 73 | }; |
| 74 | |
| 75 | if (!condition) { |
| 76 | ret = -1; |
| 77 | goto end; |
| 78 | } |
| 79 | |
| 80 | ret = sizeof(struct lttng_condition_comm); |
| 81 | if (buf) { |
| 82 | memcpy(buf, &condition_comm, ret); |
| 83 | buf += ret; |
| 84 | } |
| 85 | |
| 86 | condition_size = condition->serialize(condition, buf); |
| 87 | if (condition_size < 0) { |
| 88 | ret = condition_size; |
| 89 | goto end; |
| 90 | } |
| 91 | ret += condition_size; |
| 92 | end: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | LTTNG_HIDDEN |
| 97 | bool lttng_condition_is_equal(const struct lttng_condition *a, |
| 98 | const struct lttng_condition *b) |
| 99 | { |
| 100 | bool is_equal = false; |
| 101 | |
| 102 | if (!a || !b) { |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | if (a->type != b->type) { |
| 107 | goto end; |
| 108 | } |
| 109 | |
| 110 | is_equal = a->equal ? a->equal(a, b) : true; |
| 111 | end: |
| 112 | return is_equal; |
| 113 | } |
| 114 | |
| 115 | LTTNG_HIDDEN |
| 116 | ssize_t lttng_condition_create_from_buffer( |
| 117 | const struct lttng_buffer_view *buffer, |
| 118 | struct lttng_condition **condition) |
| 119 | { |
| 120 | ssize_t ret, condition_size = 0; |
| 121 | const struct lttng_condition_comm *condition_comm; |
| 122 | condition_create_from_buffer_cb create_from_buffer = NULL; |
| 123 | |
| 124 | if (!buffer || !condition) { |
| 125 | ret = -1; |
| 126 | goto end; |
| 127 | } |
| 128 | |
| 129 | DBG("Deserializing condition from buffer"); |
| 130 | condition_comm = (const struct lttng_condition_comm *) buffer->data; |
| 131 | condition_size += sizeof(*condition_comm); |
| 132 | |
| 133 | switch ((enum lttng_condition_type) condition_comm->condition_type) { |
| 134 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 135 | create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer; |
| 136 | break; |
| 137 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 138 | create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer; |
| 139 | break; |
| 140 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 141 | create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer; |
| 142 | break; |
| 143 | default: |
| 144 | ERR("Attempted to create condition of unknown type (%i)", |
| 145 | (int) condition_comm->condition_type); |
| 146 | ret = -1; |
| 147 | goto end; |
| 148 | } |
| 149 | |
| 150 | if (create_from_buffer) { |
| 151 | const struct lttng_buffer_view view = |
| 152 | lttng_buffer_view_from_view(buffer, |
| 153 | sizeof(*condition_comm), -1); |
| 154 | |
| 155 | ret = create_from_buffer(&view, condition); |
| 156 | if (ret < 0) { |
| 157 | goto end; |
| 158 | } |
| 159 | condition_size += ret; |
| 160 | |
| 161 | } else { |
| 162 | abort(); |
| 163 | } |
| 164 | |
| 165 | ret = condition_size; |
| 166 | end: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | LTTNG_HIDDEN |
| 171 | void lttng_condition_init(struct lttng_condition *condition, |
| 172 | enum lttng_condition_type type) |
| 173 | { |
| 174 | condition->type = type; |
| 175 | } |