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