2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/payload-view.hpp>
9 #include <common/payload.hpp>
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>
18 struct lttng_notification
*lttng_notification_create(struct lttng_trigger
*trigger
,
19 struct lttng_evaluation
*evaluation
)
21 struct lttng_notification
*notification
= nullptr;
23 if (!trigger
|| !evaluation
) {
27 notification
= zmalloc
<lttng_notification
>();
32 notification
->trigger
= trigger
;
33 notification
->evaluation
= evaluation
;
38 int lttng_notification_serialize(const struct lttng_notification
*notification
,
39 struct lttng_payload
*payload
)
42 size_t header_offset
, size_before_payload
;
43 struct lttng_notification_comm notification_comm
;
44 struct lttng_notification_comm
*header
;
46 notification_comm
.length
= 0;
48 header_offset
= payload
->buffer
.size
;
49 ret
= lttng_dynamic_buffer_append(
50 &payload
->buffer
, ¬ification_comm
, sizeof(notification_comm
));
55 size_before_payload
= payload
->buffer
.size
;
56 ret
= lttng_trigger_serialize(notification
->trigger
, payload
);
61 ret
= lttng_evaluation_serialize(notification
->evaluation
, payload
);
66 /* Update payload size. */
67 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
68 header
->length
= (uint32_t) (payload
->buffer
.size
- size_before_payload
);
73 ssize_t
lttng_notification_create_from_payload(struct lttng_payload_view
*src_view
,
74 struct lttng_notification
**notification
)
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
));
83 if (!src_view
|| !notification
) {
88 if (!lttng_payload_view_is_valid(¬ification_comm_view
)) {
89 /* Payload not large enough to contain the header. */
94 notification_comm
= (typeof(notification_comm
)) notification_comm_view
.buffer
.data
;
95 notification_size
+= sizeof(*notification_comm
);
97 /* struct lttng_condition */
98 struct lttng_payload_view condition_view
=
99 lttng_payload_view_from_view(src_view
, notification_size
, -1);
101 trigger_size
= lttng_trigger_create_from_payload(&condition_view
, &trigger
);
104 if (trigger_size
< 0) {
109 notification_size
+= trigger_size
;
112 /* struct lttng_evaluation */
113 struct lttng_payload_view evaluation_view
=
114 lttng_payload_view_from_view(src_view
, notification_size
, -1);
116 evaluation_size
= lttng_evaluation_create_from_payload(
117 lttng_trigger_get_const_condition(trigger
), &evaluation_view
, &evaluation
);
120 if (evaluation_size
< 0) {
121 ret
= evaluation_size
;
125 notification_size
+= evaluation_size
;
127 /* Unexpected size of inner-elements; the buffer is corrupted. */
128 if ((ssize_t
) notification_comm
->length
!= trigger_size
+ evaluation_size
) {
133 *notification
= lttng_notification_create(trigger
, evaluation
);
134 if (!*notification
) {
139 ret
= notification_size
;
143 lttng_trigger_destroy(trigger
);
144 lttng_evaluation_destroy(evaluation
);
148 void lttng_notification_destroy(struct lttng_notification
*notification
)
154 lttng_trigger_destroy(notification
->trigger
);
155 lttng_evaluation_destroy(notification
->evaluation
);
159 const struct lttng_condition
*
160 lttng_notification_get_condition(struct lttng_notification
*notification
)
162 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : nullptr;
165 const struct lttng_evaluation
*
166 lttng_notification_get_evaluation(struct lttng_notification
*notification
)
168 return notification
? notification
->evaluation
: nullptr;
171 const struct lttng_condition
*
172 lttng_notification_get_const_condition(const struct lttng_notification
*notification
)
174 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : nullptr;
177 const struct lttng_evaluation
*
178 lttng_notification_get_const_evaluation(const struct lttng_notification
*notification
)
180 return notification
? notification
->evaluation
: nullptr;
183 const struct lttng_trigger
*
184 lttng_notification_get_const_trigger(const struct lttng_notification
*notification
)
186 return notification
? notification
->trigger
: nullptr;
189 const struct lttng_trigger
*lttng_notification_get_trigger(struct lttng_notification
*notification
)
191 return notification
? notification
->trigger
: nullptr;