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>
16 struct lttng_notification
*lttng_notification_create(
17 struct lttng_condition
*condition
,
18 struct lttng_evaluation
*evaluation
)
20 struct lttng_notification
*notification
= NULL
;
22 if (!condition
|| !evaluation
) {
26 notification
= zmalloc(sizeof(struct lttng_notification
));
31 notification
->condition
= condition
;
32 notification
->evaluation
= evaluation
;
38 int lttng_notification_serialize(const struct lttng_notification
*notification
,
39 struct lttng_dynamic_buffer
*buf
)
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
= buf
->size
;
47 ret
= lttng_dynamic_buffer_append(buf
, ¬ification_comm
,
48 sizeof(notification_comm
));
53 size_before_payload
= buf
->size
;
54 ret
= lttng_condition_serialize(notification
->condition
,
60 ret
= lttng_evaluation_serialize(notification
->evaluation
, buf
);
65 /* Update payload size. */
66 header
= (struct lttng_notification_comm
*) ((char *) buf
->data
+ header_offset
);
67 header
->length
= (uint32_t) (buf
->size
- size_before_payload
);
74 ssize_t
lttng_notification_create_from_buffer(
75 const struct lttng_buffer_view
*src_view
,
76 struct lttng_notification
**notification
)
78 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
79 const struct lttng_notification_comm
*notification_comm
;
80 struct lttng_condition
*condition
;
81 struct lttng_evaluation
*evaluation
;
82 struct lttng_buffer_view condition_view
;
83 struct lttng_buffer_view evaluation_view
;
85 if (!src_view
|| !notification
) {
91 (const struct lttng_notification_comm
*) src_view
->data
;
92 notification_size
+= sizeof(*notification_comm
);
94 /* struct lttng_condition */
95 condition_view
= lttng_buffer_view_from_view(src_view
,
96 sizeof(*notification_comm
), -1);
97 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
99 if (condition_size
< 0) {
100 ret
= condition_size
;
103 notification_size
+= condition_size
;
105 /* struct lttng_evaluation */
106 evaluation_view
= lttng_buffer_view_from_view(&condition_view
,
108 evaluation_size
= lttng_evaluation_create_from_buffer(&evaluation_view
,
110 if (evaluation_size
< 0) {
111 ret
= evaluation_size
;
114 notification_size
+= evaluation_size
;
116 /* Unexpected size of inner-elements; the buffer is corrupted. */
117 if ((ssize_t
) notification_comm
->length
!=
118 condition_size
+ evaluation_size
) {
123 *notification
= lttng_notification_create(condition
, evaluation
);
124 if (!*notification
) {
128 ret
= notification_size
;
132 lttng_condition_destroy(condition
);
133 lttng_evaluation_destroy(evaluation
);
137 void lttng_notification_destroy(struct lttng_notification
*notification
)
143 lttng_condition_destroy(notification
->condition
);
144 lttng_evaluation_destroy(notification
->evaluation
);
148 const struct lttng_condition
*lttng_notification_get_condition(
149 struct lttng_notification
*notification
)
151 return notification
? notification
->condition
: NULL
;
154 const struct lttng_evaluation
*lttng_notification_get_evaluation(
155 struct lttng_notification
*notification
)
157 return notification
? notification
->evaluation
: NULL
;