2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <lttng/notification/notification-internal.h>
19 #include <lttng/condition/condition-internal.h>
20 #include <lttng/condition/evaluation-internal.h>
21 #include <lttng/condition/condition.h>
22 #include <lttng/condition/evaluation.h>
26 struct lttng_notification
*lttng_notification_create(
27 struct lttng_condition
*condition
,
28 struct lttng_evaluation
*evaluation
)
30 struct lttng_notification
*notification
= NULL
;
32 if (!condition
|| !evaluation
) {
36 notification
= zmalloc(sizeof(struct lttng_notification
));
41 notification
->condition
= condition
;
42 notification
->evaluation
= evaluation
;
48 int lttng_notification_serialize(const struct lttng_notification
*notification
,
49 struct lttng_dynamic_buffer
*buf
)
52 size_t header_offset
, size_before_payload
;
53 struct lttng_notification_comm notification_comm
= { 0 };
54 struct lttng_notification_comm
*header
;
56 header_offset
= buf
->size
;
57 ret
= lttng_dynamic_buffer_append(buf
, ¬ification_comm
,
58 sizeof(notification_comm
));
63 size_before_payload
= buf
->size
;
64 ret
= lttng_condition_serialize(notification
->condition
,
70 ret
= lttng_evaluation_serialize(notification
->evaluation
, buf
);
75 /* Update payload size. */
76 header
= (struct lttng_notification_comm
*) ((char *) buf
->data
+ header_offset
);
77 header
->length
= (uint32_t) (buf
->size
- size_before_payload
);
84 ssize_t
lttng_notification_create_from_buffer(
85 const struct lttng_buffer_view
*src_view
,
86 struct lttng_notification
**notification
)
88 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
89 const struct lttng_notification_comm
*notification_comm
;
90 struct lttng_condition
*condition
;
91 struct lttng_evaluation
*evaluation
;
92 struct lttng_buffer_view condition_view
;
93 struct lttng_buffer_view evaluation_view
;
95 if (!src_view
|| !notification
) {
101 (const struct lttng_notification_comm
*) src_view
->data
;
102 notification_size
+= sizeof(*notification_comm
);
104 /* struct lttng_condition */
105 condition_view
= lttng_buffer_view_from_view(src_view
,
106 sizeof(*notification_comm
), -1);
107 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
109 if (condition_size
< 0) {
110 ret
= condition_size
;
113 notification_size
+= condition_size
;
115 /* struct lttng_evaluation */
116 evaluation_view
= lttng_buffer_view_from_view(&condition_view
,
118 evaluation_size
= lttng_evaluation_create_from_buffer(&evaluation_view
,
120 if (evaluation_size
< 0) {
121 ret
= evaluation_size
;
124 notification_size
+= evaluation_size
;
126 /* Unexpected size of inner-elements; the buffer is corrupted. */
127 if ((ssize_t
) notification_comm
->length
!=
128 condition_size
+ evaluation_size
) {
133 *notification
= lttng_notification_create(condition
, evaluation
);
134 if (!*notification
) {
138 ret
= notification_size
;
142 lttng_condition_destroy(condition
);
143 lttng_evaluation_destroy(evaluation
);
147 void lttng_notification_destroy(struct lttng_notification
*notification
)
153 lttng_condition_destroy(notification
->condition
);
154 lttng_evaluation_destroy(notification
->evaluation
);
158 const struct lttng_condition
*lttng_notification_get_condition(
159 struct lttng_notification
*notification
)
161 return notification
? notification
->condition
: NULL
;
164 const struct lttng_evaluation
*lttng_notification_get_evaluation(
165 struct lttng_notification
*notification
)
167 return notification
? notification
->evaluation
: NULL
;