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
;
43 notification
->owns_elements
= false;
49 ssize_t
lttng_notification_serialize(struct lttng_notification
*notification
,
52 ssize_t ret
, condition_size
, evaluation_size
, offset
= 0;
53 struct lttng_notification_comm notification_comm
;
60 offset
+= sizeof(notification_comm
);
61 condition_size
= lttng_condition_serialize(notification
->condition
,
62 buf
? (buf
+ offset
) : NULL
);
63 if (condition_size
< 0) {
67 offset
+= condition_size
;
69 evaluation_size
= lttng_evaluation_serialize(notification
->evaluation
,
70 buf
? (buf
+ offset
) : NULL
);
71 if (evaluation_size
< 0) {
72 ret
= evaluation_size
;
75 offset
+= evaluation_size
;
78 notification_comm
.length
=
79 (uint32_t) (condition_size
+ evaluation_size
);
80 memcpy(buf
, ¬ification_comm
, sizeof(notification_comm
));
89 ssize_t
lttng_notification_create_from_buffer(
90 const struct lttng_buffer_view
*src_view
,
91 struct lttng_notification
**notification
)
93 ssize_t ret
, notification_size
= 0, condition_size
, evaluation_size
;
94 const struct lttng_notification_comm
*notification_comm
;
95 struct lttng_condition
*condition
;
96 struct lttng_evaluation
*evaluation
;
97 struct lttng_buffer_view condition_view
;
98 struct lttng_buffer_view evaluation_view
;
100 if (!src_view
|| !notification
) {
106 (const struct lttng_notification_comm
*) src_view
->data
;
107 notification_size
+= sizeof(*notification_comm
);
109 /* struct lttng_condition */
110 condition_view
= lttng_buffer_view_from_view(src_view
,
111 sizeof(*notification_comm
), -1);
112 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
114 if (condition_size
< 0) {
115 ret
= condition_size
;
118 notification_size
+= condition_size
;
120 /* struct lttng_evaluation */
121 evaluation_view
= lttng_buffer_view_from_view(&condition_view
,
123 evaluation_size
= lttng_evaluation_create_from_buffer(&evaluation_view
,
125 if (evaluation_size
< 0) {
126 ret
= evaluation_size
;
129 notification_size
+= evaluation_size
;
131 /* Unexpected size of inner-elements; the buffer is corrupted. */
132 if ((ssize_t
) notification_comm
->length
!=
133 condition_size
+ evaluation_size
) {
138 *notification
= lttng_notification_create(condition
, evaluation
);
139 if (!*notification
) {
143 ret
= notification_size
;
144 (*notification
)->owns_elements
= true;
148 lttng_condition_destroy(condition
);
149 lttng_evaluation_destroy(evaluation
);
153 void lttng_notification_destroy(struct lttng_notification
*notification
)
159 if (notification
->owns_elements
) {
160 lttng_condition_destroy(notification
->condition
);
161 lttng_evaluation_destroy(notification
->evaluation
);
166 const struct lttng_condition
*lttng_notification_get_condition(
167 struct lttng_notification
*notification
)
169 return notification
? notification
->condition
: NULL
;
172 const struct lttng_evaluation
*lttng_notification_get_evaluation(
173 struct lttng_notification
*notification
)
175 return notification
? notification
->evaluation
: NULL
;