2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <lttng/notification/notification-internal.hpp>
9 #include <lttng/condition/condition-internal.hpp>
10 #include <lttng/condition/evaluation-internal.hpp>
11 #include <lttng/condition/condition.h>
12 #include <lttng/condition/evaluation.h>
13 #include <lttng/trigger/trigger-internal.hpp>
14 #include <common/payload.hpp>
15 #include <common/payload-view.hpp>
17 struct lttng_notification
*lttng_notification_create(
18 struct lttng_trigger
*trigger
,
19 struct lttng_evaluation
*evaluation
)
21 struct lttng_notification
*notification
= NULL
;
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(&payload
->buffer
, ¬ification_comm
,
50 sizeof(notification_comm
));
55 size_before_payload
= payload
->buffer
.size
;
56 ret
= lttng_trigger_serialize(notification
->trigger
,
62 ret
= lttng_evaluation_serialize(notification
->evaluation
, payload
);
67 /* Update payload size. */
68 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
69 header
->length
= (uint32_t) (payload
->buffer
.size
- size_before_payload
);
75 ssize_t
lttng_notification_create_from_payload(
76 struct lttng_payload_view
*src_view
,
77 struct lttng_notification
**notification
)
79 ssize_t ret
, notification_size
= 0, trigger_size
, evaluation_size
;
80 struct lttng_trigger
*trigger
= NULL
;
81 struct lttng_evaluation
*evaluation
= NULL
;
82 const struct lttng_notification_comm
*notification_comm
;
83 const struct lttng_payload_view notification_comm_view
=
84 lttng_payload_view_from_view(
85 src_view
, 0, sizeof(*notification_comm
));
87 if (!src_view
|| !notification
) {
92 if (!lttng_payload_view_is_valid(¬ification_comm_view
)) {
93 /* Payload not large enough to contain the header. */
98 notification_comm
= (typeof(notification_comm
)) notification_comm_view
.buffer
.data
;
99 notification_size
+= sizeof(*notification_comm
);
101 /* struct lttng_condition */
102 struct lttng_payload_view condition_view
=
103 lttng_payload_view_from_view(src_view
,
104 notification_size
, -1);
106 trigger_size
= lttng_trigger_create_from_payload(
107 &condition_view
, &trigger
);
110 if (trigger_size
< 0) {
115 notification_size
+= trigger_size
;
118 /* struct lttng_evaluation */
119 struct lttng_payload_view evaluation_view
=
120 lttng_payload_view_from_view(src_view
,
121 notification_size
, -1);
123 evaluation_size
= lttng_evaluation_create_from_payload(
124 lttng_trigger_get_const_condition(trigger
),
125 &evaluation_view
, &evaluation
);
128 if (evaluation_size
< 0) {
129 ret
= evaluation_size
;
133 notification_size
+= evaluation_size
;
135 /* Unexpected size of inner-elements; the buffer is corrupted. */
136 if ((ssize_t
) notification_comm
->length
!=
137 trigger_size
+ evaluation_size
) {
142 *notification
= lttng_notification_create(trigger
, evaluation
);
143 if (!*notification
) {
148 ret
= notification_size
;
152 lttng_trigger_destroy(trigger
);
153 lttng_evaluation_destroy(evaluation
);
157 void lttng_notification_destroy(struct lttng_notification
*notification
)
163 lttng_trigger_destroy(notification
->trigger
);
164 lttng_evaluation_destroy(notification
->evaluation
);
168 const struct lttng_condition
*lttng_notification_get_condition(
169 struct lttng_notification
*notification
)
171 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : NULL
;
174 const struct lttng_evaluation
*lttng_notification_get_evaluation(
175 struct lttng_notification
*notification
)
177 return notification
? notification
->evaluation
: NULL
;
180 const struct lttng_condition
*lttng_notification_get_const_condition(
181 const struct lttng_notification
*notification
)
183 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : NULL
;
186 const struct lttng_evaluation
*lttng_notification_get_const_evaluation(
187 const struct lttng_notification
*notification
)
189 return notification
? notification
->evaluation
: NULL
;
192 const struct lttng_trigger
*lttng_notification_get_const_trigger(
193 const struct lttng_notification
*notification
)
195 return notification
? notification
->trigger
: NULL
;
198 const struct lttng_trigger
*lttng_notification_get_trigger(
199 struct lttng_notification
*notification
)
201 return notification
? notification
->trigger
: NULL
;