| 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 <assert.h> |
| 14 | |
| 15 | LTTNG_HIDDEN |
| 16 | struct lttng_notification *lttng_notification_create( |
| 17 | struct lttng_condition *condition, |
| 18 | struct lttng_evaluation *evaluation) |
| 19 | { |
| 20 | struct lttng_notification *notification = NULL; |
| 21 | |
| 22 | if (!condition || !evaluation) { |
| 23 | goto end; |
| 24 | } |
| 25 | |
| 26 | notification = zmalloc(sizeof(struct lttng_notification)); |
| 27 | if (!notification) { |
| 28 | goto end; |
| 29 | } |
| 30 | |
| 31 | notification->condition = condition; |
| 32 | notification->evaluation = evaluation; |
| 33 | end: |
| 34 | return notification; |
| 35 | } |
| 36 | |
| 37 | LTTNG_HIDDEN |
| 38 | int lttng_notification_serialize(const struct lttng_notification *notification, |
| 39 | struct lttng_dynamic_buffer *buf) |
| 40 | { |
| 41 | int ret; |
| 42 | size_t header_offset, size_before_payload; |
| 43 | struct lttng_notification_comm notification_comm = { 0 }; |
| 44 | struct lttng_notification_comm *header; |
| 45 | |
| 46 | header_offset = buf->size; |
| 47 | ret = lttng_dynamic_buffer_append(buf, ¬ification_comm, |
| 48 | sizeof(notification_comm)); |
| 49 | if (ret) { |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | size_before_payload = buf->size; |
| 54 | ret = lttng_condition_serialize(notification->condition, |
| 55 | buf); |
| 56 | if (ret) { |
| 57 | goto end; |
| 58 | } |
| 59 | |
| 60 | ret = lttng_evaluation_serialize(notification->evaluation, buf); |
| 61 | if (ret) { |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | /* Update payload size. */ |
| 66 | header = (struct lttng_notification_comm *) ((char *) buf->data + header_offset); |
| 67 | header->length = (uint32_t) (buf->size - size_before_payload); |
| 68 | end: |
| 69 | return ret; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | LTTNG_HIDDEN |
| 74 | ssize_t lttng_notification_create_from_buffer( |
| 75 | const struct lttng_buffer_view *src_view, |
| 76 | struct lttng_notification **notification) |
| 77 | { |
| 78 | ssize_t ret, notification_size = 0, condition_size, evaluation_size; |
| 79 | const struct lttng_notification_comm *notification_comm; |
| 80 | struct lttng_condition *condition; |
| 81 | struct lttng_evaluation *evaluation; |
| 82 | struct lttng_buffer_view condition_view; |
| 83 | struct lttng_buffer_view evaluation_view; |
| 84 | |
| 85 | if (!src_view || !notification) { |
| 86 | ret = -1; |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | notification_comm = |
| 91 | (const struct lttng_notification_comm *) src_view->data; |
| 92 | notification_size += sizeof(*notification_comm); |
| 93 | |
| 94 | /* struct lttng_condition */ |
| 95 | condition_view = lttng_buffer_view_from_view(src_view, |
| 96 | sizeof(*notification_comm), -1); |
| 97 | condition_size = lttng_condition_create_from_buffer(&condition_view, |
| 98 | &condition); |
| 99 | if (condition_size < 0) { |
| 100 | ret = condition_size; |
| 101 | goto end; |
| 102 | } |
| 103 | notification_size += condition_size; |
| 104 | |
| 105 | /* struct lttng_evaluation */ |
| 106 | evaluation_view = lttng_buffer_view_from_view(&condition_view, |
| 107 | condition_size, -1); |
| 108 | evaluation_size = lttng_evaluation_create_from_buffer(&evaluation_view, |
| 109 | &evaluation); |
| 110 | if (evaluation_size < 0) { |
| 111 | ret = evaluation_size; |
| 112 | goto end; |
| 113 | } |
| 114 | notification_size += evaluation_size; |
| 115 | |
| 116 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 117 | if ((ssize_t) notification_comm->length != |
| 118 | condition_size + evaluation_size) { |
| 119 | ret = -1; |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | *notification = lttng_notification_create(condition, evaluation); |
| 124 | if (!*notification) { |
| 125 | ret = -1; |
| 126 | goto error; |
| 127 | } |
| 128 | ret = notification_size; |
| 129 | end: |
| 130 | return ret; |
| 131 | error: |
| 132 | lttng_condition_destroy(condition); |
| 133 | lttng_evaluation_destroy(evaluation); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | void lttng_notification_destroy(struct lttng_notification *notification) |
| 138 | { |
| 139 | if (!notification) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | lttng_condition_destroy(notification->condition); |
| 144 | lttng_evaluation_destroy(notification->evaluation); |
| 145 | free(notification); |
| 146 | } |
| 147 | |
| 148 | const struct lttng_condition *lttng_notification_get_condition( |
| 149 | struct lttng_notification *notification) |
| 150 | { |
| 151 | return notification ? notification->condition : NULL; |
| 152 | } |
| 153 | |
| 154 | const struct lttng_evaluation *lttng_notification_get_evaluation( |
| 155 | struct lttng_notification *notification) |
| 156 | { |
| 157 | return notification ? notification->evaluation : NULL; |
| 158 | } |