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