| 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 <common/payload-view.hpp> |
| 9 | #include <common/payload.hpp> |
| 10 | |
| 11 | #include <lttng/condition/condition-internal.hpp> |
| 12 | #include <lttng/condition/condition.h> |
| 13 | #include <lttng/condition/evaluation-internal.hpp> |
| 14 | #include <lttng/condition/evaluation.h> |
| 15 | #include <lttng/notification/notification-internal.hpp> |
| 16 | #include <lttng/trigger/trigger-internal.hpp> |
| 17 | |
| 18 | struct lttng_notification *lttng_notification_create(struct lttng_trigger *trigger, |
| 19 | struct lttng_evaluation *evaluation) |
| 20 | { |
| 21 | struct lttng_notification *notification = nullptr; |
| 22 | |
| 23 | if (!trigger || !evaluation) { |
| 24 | goto end; |
| 25 | } |
| 26 | |
| 27 | notification = zmalloc<lttng_notification>(); |
| 28 | if (!notification) { |
| 29 | goto end; |
| 30 | } |
| 31 | |
| 32 | notification->trigger = trigger; |
| 33 | notification->evaluation = evaluation; |
| 34 | end: |
| 35 | return notification; |
| 36 | } |
| 37 | |
| 38 | int lttng_notification_serialize(const struct lttng_notification *notification, |
| 39 | struct lttng_payload *payload) |
| 40 | { |
| 41 | int ret; |
| 42 | size_t header_offset, size_before_payload; |
| 43 | struct lttng_notification_comm notification_comm; |
| 44 | struct lttng_notification_comm *header; |
| 45 | |
| 46 | notification_comm.length = 0; |
| 47 | |
| 48 | header_offset = payload->buffer.size; |
| 49 | ret = lttng_dynamic_buffer_append( |
| 50 | &payload->buffer, ¬ification_comm, sizeof(notification_comm)); |
| 51 | if (ret) { |
| 52 | goto end; |
| 53 | } |
| 54 | |
| 55 | size_before_payload = payload->buffer.size; |
| 56 | ret = lttng_trigger_serialize(notification->trigger, payload); |
| 57 | if (ret) { |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | ret = lttng_evaluation_serialize(notification->evaluation, payload); |
| 62 | if (ret) { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | /* Update payload size. */ |
| 67 | header = (typeof(header)) (payload->buffer.data + header_offset); |
| 68 | header->length = (uint32_t) (payload->buffer.size - size_before_payload); |
| 69 | end: |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | ssize_t lttng_notification_create_from_payload(struct lttng_payload_view *src_view, |
| 74 | struct lttng_notification **notification) |
| 75 | { |
| 76 | ssize_t ret, notification_size = 0, trigger_size, evaluation_size; |
| 77 | struct lttng_trigger *trigger = nullptr; |
| 78 | struct lttng_evaluation *evaluation = nullptr; |
| 79 | const struct lttng_notification_comm *notification_comm; |
| 80 | const struct lttng_payload_view notification_comm_view = |
| 81 | lttng_payload_view_from_view(src_view, 0, sizeof(*notification_comm)); |
| 82 | |
| 83 | if (!src_view || !notification) { |
| 84 | ret = -1; |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | if (!lttng_payload_view_is_valid(¬ification_comm_view)) { |
| 89 | /* Payload not large enough to contain the header. */ |
| 90 | ret = -1; |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data; |
| 95 | notification_size += sizeof(*notification_comm); |
| 96 | { |
| 97 | /* struct lttng_condition */ |
| 98 | struct lttng_payload_view condition_view = |
| 99 | lttng_payload_view_from_view(src_view, notification_size, -1); |
| 100 | |
| 101 | trigger_size = lttng_trigger_create_from_payload(&condition_view, &trigger); |
| 102 | } |
| 103 | |
| 104 | if (trigger_size < 0) { |
| 105 | ret = trigger_size; |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | notification_size += trigger_size; |
| 110 | |
| 111 | { |
| 112 | /* struct lttng_evaluation */ |
| 113 | struct lttng_payload_view evaluation_view = |
| 114 | lttng_payload_view_from_view(src_view, notification_size, -1); |
| 115 | |
| 116 | evaluation_size = lttng_evaluation_create_from_payload( |
| 117 | lttng_trigger_get_const_condition(trigger), &evaluation_view, &evaluation); |
| 118 | } |
| 119 | |
| 120 | if (evaluation_size < 0) { |
| 121 | ret = evaluation_size; |
| 122 | goto error; |
| 123 | } |
| 124 | |
| 125 | notification_size += evaluation_size; |
| 126 | |
| 127 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 128 | if ((ssize_t) notification_comm->length != trigger_size + evaluation_size) { |
| 129 | ret = -1; |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | *notification = lttng_notification_create(trigger, evaluation); |
| 134 | if (!*notification) { |
| 135 | ret = -1; |
| 136 | goto error; |
| 137 | } |
| 138 | |
| 139 | ret = notification_size; |
| 140 | return ret; |
| 141 | |
| 142 | error: |
| 143 | lttng_trigger_destroy(trigger); |
| 144 | lttng_evaluation_destroy(evaluation); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | void lttng_notification_destroy(struct lttng_notification *notification) |
| 149 | { |
| 150 | if (!notification) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | lttng_trigger_destroy(notification->trigger); |
| 155 | lttng_evaluation_destroy(notification->evaluation); |
| 156 | free(notification); |
| 157 | } |
| 158 | |
| 159 | const struct lttng_condition * |
| 160 | lttng_notification_get_condition(struct lttng_notification *notification) |
| 161 | { |
| 162 | return notification ? lttng_trigger_get_const_condition(notification->trigger) : nullptr; |
| 163 | } |
| 164 | |
| 165 | const struct lttng_evaluation * |
| 166 | lttng_notification_get_evaluation(struct lttng_notification *notification) |
| 167 | { |
| 168 | return notification ? notification->evaluation : nullptr; |
| 169 | } |
| 170 | |
| 171 | const struct lttng_condition * |
| 172 | lttng_notification_get_const_condition(const struct lttng_notification *notification) |
| 173 | { |
| 174 | return notification ? lttng_trigger_get_const_condition(notification->trigger) : nullptr; |
| 175 | } |
| 176 | |
| 177 | const struct lttng_evaluation * |
| 178 | lttng_notification_get_const_evaluation(const struct lttng_notification *notification) |
| 179 | { |
| 180 | return notification ? notification->evaluation : nullptr; |
| 181 | } |
| 182 | |
| 183 | const struct lttng_trigger * |
| 184 | lttng_notification_get_const_trigger(const struct lttng_notification *notification) |
| 185 | { |
| 186 | return notification ? notification->trigger : nullptr; |
| 187 | } |
| 188 | |
| 189 | const struct lttng_trigger *lttng_notification_get_trigger(struct lttng_notification *notification) |
| 190 | { |
| 191 | return notification ? notification->trigger : nullptr; |
| 192 | } |