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
, buf
, NULL
);
59 ret
= lttng_evaluation_serialize(notification
->evaluation
, buf
);
64 /* Update payload size. */
65 header
= (struct lttng_notification_comm
*) ((char *) buf
->data
+ header_offset
);
66 header
->length
= (uint32_t) (buf
->size
- size_before_payload
);
73 ssize_t
lttng_notification_create_from_buffer(
74 const struct lttng_buffer_view
*src_view
,
75 struct lttng_notification
**notification
)
77 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
78 const struct lttng_notification_comm
*notification_comm
;
79 struct lttng_condition
*condition
;
80 struct lttng_evaluation
*evaluation
;
81 struct lttng_buffer_view condition_view
;
82 struct lttng_buffer_view evaluation_view
;
84 if (!src_view
|| !notification
) {
90 (const struct lttng_notification_comm
*) src_view
->data
;
91 notification_size
+= sizeof(*notification_comm
);
93 /* struct lttng_condition */
94 condition_view
= lttng_buffer_view_from_view(src_view
,
95 sizeof(*notification_comm
), -1);
96 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
98 if (condition_size
< 0) {
102 notification_size
+= condition_size
;
104 /* struct lttng_evaluation */
105 evaluation_view
= lttng_buffer_view_from_view(&condition_view
,
107 evaluation_size
= lttng_evaluation_create_from_buffer(condition
,
108 &evaluation_view
, &evaluation
);
109 if (evaluation_size
< 0) {
110 ret
= evaluation_size
;
113 notification_size
+= evaluation_size
;
115 /* Unexpected size of inner-elements; the buffer is corrupted. */
116 if ((ssize_t
) notification_comm
->length
!=
117 condition_size
+ evaluation_size
) {
122 *notification
= lttng_notification_create(condition
, evaluation
);
123 if (!*notification
) {
127 ret
= notification_size
;
131 lttng_condition_destroy(condition
);
132 lttng_evaluation_destroy(evaluation
);
136 void lttng_notification_destroy(struct lttng_notification
*notification
)
142 lttng_condition_destroy(notification
->condition
);
143 lttng_evaluation_destroy(notification
->evaluation
);
147 const struct lttng_condition
*lttng_notification_get_condition(
148 struct lttng_notification
*notification
)
150 return notification
? notification
->condition
: NULL
;
153 const struct lttng_evaluation
*lttng_notification_get_evaluation(
154 struct lttng_notification
*notification
)
156 return notification
? notification
->evaluation
: NULL
;