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.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 <common/payload.h>
14 #include <common/payload-view.h>
18 struct lttng_notification
*lttng_notification_create(
19 struct lttng_condition
*condition
,
20 struct lttng_evaluation
*evaluation
)
22 struct lttng_notification
*notification
= NULL
;
24 if (!condition
|| !evaluation
) {
28 notification
= zmalloc(sizeof(struct lttng_notification
));
33 notification
->condition
= condition
;
34 notification
->evaluation
= evaluation
;
40 int lttng_notification_serialize(const struct lttng_notification
*notification
,
41 struct lttng_payload
*payload
)
44 size_t header_offset
, size_before_payload
;
45 struct lttng_notification_comm notification_comm
= { 0 };
46 struct lttng_notification_comm
*header
;
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_condition_serialize(notification
->condition
,
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
);
76 ssize_t
lttng_notification_create_from_payload(
77 struct lttng_payload_view
*src_view
,
78 struct lttng_notification
**notification
)
80 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
81 struct lttng_condition
*condition
;
82 struct lttng_evaluation
*evaluation
;
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
));
88 if (!src_view
|| !notification
) {
93 if (!lttng_payload_view_is_valid(¬ification_comm_view
)) {
94 /* Payload not large enough to contain the header. */
99 notification_comm
= (typeof(notification_comm
)) notification_comm_view
.buffer
.data
;
100 notification_size
+= sizeof(*notification_comm
);
102 /* struct lttng_condition */
103 struct lttng_payload_view condition_view
=
104 lttng_payload_view_from_view(src_view
,
105 notification_size
, -1);
107 condition_size
= lttng_condition_create_from_payload(
108 &condition_view
, &condition
);
111 if (condition_size
< 0) {
112 ret
= condition_size
;
116 notification_size
+= condition_size
;
119 /* struct lttng_evaluation */
120 struct lttng_payload_view evaluation_view
=
121 lttng_payload_view_from_view(src_view
,
122 notification_size
, -1);
124 evaluation_size
= lttng_evaluation_create_from_payload(
125 condition
, &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 condition_size
+ evaluation_size
) {
142 *notification
= lttng_notification_create(condition
, evaluation
);
143 if (!*notification
) {
147 ret
= notification_size
;
151 lttng_condition_destroy(condition
);
152 lttng_evaluation_destroy(evaluation
);
156 void lttng_notification_destroy(struct lttng_notification
*notification
)
162 lttng_condition_destroy(notification
->condition
);
163 lttng_evaluation_destroy(notification
->evaluation
);
167 const struct lttng_condition
*lttng_notification_get_condition(
168 struct lttng_notification
*notification
)
170 return notification
? notification
->condition
: NULL
;
173 const struct lttng_evaluation
*lttng_notification_get_evaluation(
174 struct lttng_notification
*notification
)
176 return notification
? notification
->evaluation
: NULL
;