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 <lttng/trigger/trigger-internal.h>
14 #include <common/payload.h>
15 #include <common/payload-view.h>
19 struct lttng_notification
*lttng_notification_create(
20 struct lttng_trigger
*trigger
,
21 struct lttng_evaluation
*evaluation
)
23 struct lttng_notification
*notification
= NULL
;
25 if (!trigger
|| !evaluation
) {
29 notification
= zmalloc(sizeof(struct lttng_notification
));
34 notification
->trigger
= trigger
;
35 notification
->evaluation
= evaluation
;
41 int lttng_notification_serialize(const struct lttng_notification
*notification
,
42 struct lttng_payload
*payload
)
45 size_t header_offset
, size_before_payload
;
46 struct lttng_notification_comm notification_comm
= { 0 };
47 struct lttng_notification_comm
*header
;
49 header_offset
= payload
->buffer
.size
;
50 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, ¬ification_comm
,
51 sizeof(notification_comm
));
56 size_before_payload
= payload
->buffer
.size
;
57 ret
= lttng_trigger_serialize(notification
->trigger
,
63 ret
= lttng_evaluation_serialize(notification
->evaluation
, payload
);
68 /* Update payload size. */
69 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
70 header
->length
= (uint32_t) (payload
->buffer
.size
- size_before_payload
);
77 ssize_t
lttng_notification_create_from_payload(
78 struct lttng_payload_view
*src_view
,
79 struct lttng_notification
**notification
)
81 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
82 struct lttng_trigger
*trigger
;
83 struct lttng_evaluation
*evaluation
;
84 const struct lttng_notification_comm
*notification_comm
;
85 const struct lttng_payload_view notification_comm_view
=
86 lttng_payload_view_from_view(
87 src_view
, 0, sizeof(*notification_comm
));
89 if (!src_view
|| !notification
) {
94 if (!lttng_payload_view_is_valid(¬ification_comm_view
)) {
95 /* Payload not large enough to contain the header. */
100 notification_comm
= (typeof(notification_comm
)) notification_comm_view
.buffer
.data
;
101 notification_size
+= sizeof(*notification_comm
);
103 /* struct lttng_condition */
104 struct lttng_payload_view condition_view
=
105 lttng_payload_view_from_view(src_view
,
106 notification_size
, -1);
108 condition_size
= lttng_trigger_create_from_payload(
109 &condition_view
, &trigger
);
112 if (condition_size
< 0) {
113 ret
= condition_size
;
117 notification_size
+= condition_size
;
120 /* struct lttng_evaluation */
121 struct lttng_payload_view evaluation_view
=
122 lttng_payload_view_from_view(src_view
,
123 notification_size
, -1);
125 evaluation_size
= lttng_evaluation_create_from_payload(
126 lttng_trigger_get_const_condition(trigger
),
127 &evaluation_view
, &evaluation
);
130 if (evaluation_size
< 0) {
131 ret
= evaluation_size
;
135 notification_size
+= evaluation_size
;
137 /* Unexpected size of inner-elements; the buffer is corrupted. */
138 if ((ssize_t
) notification_comm
->length
!=
139 condition_size
+ evaluation_size
) {
144 *notification
= lttng_notification_create(trigger
, evaluation
);
145 if (!*notification
) {
149 ret
= notification_size
;
153 lttng_trigger_destroy(trigger
);
154 lttng_evaluation_destroy(evaluation
);
158 void lttng_notification_destroy(struct lttng_notification
*notification
)
164 lttng_trigger_destroy(notification
->trigger
);
165 lttng_evaluation_destroy(notification
->evaluation
);
169 const struct lttng_condition
*lttng_notification_get_condition(
170 struct lttng_notification
*notification
)
172 return notification
? lttng_trigger_get_const_condition(notification
->trigger
) : NULL
;
175 const struct lttng_evaluation
*lttng_notification_get_evaluation(
176 struct lttng_notification
*notification
)
178 return notification
? notification
->evaluation
: NULL
;
181 const struct lttng_trigger
*lttng_notification_get_trigger(
182 struct lttng_notification
*notification
)
184 return notification
? notification
->trigger
: NULL
;