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
= { 0 };
44 struct lttng_notification_comm
*header
;
46 header_offset
= payload
->buffer
.size
;
47 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ¬ification_comm
,
48 sizeof(notification_comm
));
53 size_before_payload
= payload
->buffer
.size
;
54 ret
= lttng_trigger_serialize(notification
->trigger
,
60 ret
= lttng_evaluation_serialize(notification
->evaluation
, payload
);
65 /* Update payload size. */
66 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
67 header
->length
= (uint32_t) (payload
->buffer
.size
- size_before_payload
);
73 ssize_t
lttng_notification_create_from_payload(
74 struct lttng_payload_view
*src_view
,
75 struct lttng_notification
**notification
)
77 ssize_t ret
, notification_size
= 0, trigger_size
, evaluation_size
;
78 struct lttng_trigger
*trigger
= NULL
;
79 struct lttng_evaluation
*evaluation
= NULL
;
80 const struct lttng_notification_comm
*notification_comm
;
81 const struct lttng_payload_view notification_comm_view
=
82 lttng_payload_view_from_view(
83 src_view
, 0, sizeof(*notification_comm
));
85 if (!src_view
|| !notification
) {
90 if (!lttng_payload_view_is_valid(¬ification_comm_view
)) {
91 /* Payload not large enough to contain the header. */
96 notification_comm
= (typeof(notification_comm
)) notification_comm_view
.buffer
.data
;
97 notification_size
+= sizeof(*notification_comm
);
99 /* struct lttng_condition */
100 struct lttng_payload_view condition_view
=
101 lttng_payload_view_from_view(src_view
,
102 notification_size
, -1);
104 trigger_size
= lttng_trigger_create_from_payload(
105 &condition_view
, &trigger
);
108 if (trigger_size
< 0) {
113 notification_size
+= trigger_size
;
116 /* struct lttng_evaluation */
117 struct lttng_payload_view evaluation_view
=
118 lttng_payload_view_from_view(src_view
,
119 notification_size
, -1);
121 evaluation_size
= lttng_evaluation_create_from_payload(
122 lttng_trigger_get_const_condition(trigger
),
123 &evaluation_view
, &evaluation
);
126 if (evaluation_size
< 0) {
127 ret
= evaluation_size
;
131 notification_size
+= evaluation_size
;
133 /* Unexpected size of inner-elements; the buffer is corrupted. */
134 if ((ssize_t
) notification_comm
->length
!=
135 trigger_size
+ evaluation_size
) {
140 *notification
= lttng_notification_create(trigger
, evaluation
);
141 if (!*notification
) {
146 ret
= notification_size
;
150 lttng_trigger_destroy(trigger
);
151 lttng_evaluation_destroy(evaluation
);
155 void lttng_notification_destroy(struct lttng_notification
*notification
)
161 lttng_trigger_destroy(notification
->trigger
);
162 lttng_evaluation_destroy(notification
->evaluation
);
166 const struct lttng_condition
*lttng_notification_get_condition(
167 struct lttng_notification
*notification
)
169 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : NULL
;
172 const struct lttng_evaluation
*lttng_notification_get_evaluation(
173 struct lttng_notification
*notification
)
175 return notification
? notification
->evaluation
: NULL
;
178 const struct lttng_trigger
*lttng_notification_get_trigger(
179 struct lttng_notification
*notification
)
181 return notification
? notification
->trigger
: NULL
;