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(const char *buf
,
90 struct lttng_notification
**notification
)
92 ssize_t ret
, offset
= 0, condition_size
, evaluation_size
;
93 struct lttng_notification_comm
*notification_comm
;
94 struct lttng_condition
*condition
;
95 struct lttng_evaluation
*evaluation
;
97 if (!buf
|| !notification
) {
102 notification_comm
= (struct lttng_notification_comm
*) buf
;
103 offset
+= sizeof(*notification_comm
);
105 /* struct lttng_condition */
106 condition_size
= lttng_condition_create_from_buffer(buf
+ offset
,
108 if (condition_size
< 0) {
109 ret
= condition_size
;
112 offset
+= condition_size
;
114 /* struct lttng_evaluation */
115 evaluation_size
= lttng_evaluation_create_from_buffer(buf
+ offset
,
117 if (evaluation_size
< 0) {
118 ret
= evaluation_size
;
121 offset
+= evaluation_size
;
123 /* Unexpected size of inner-elements; the buffer is corrupted. */
124 if ((ssize_t
) notification_comm
->length
!=
125 condition_size
+ evaluation_size
) {
130 *notification
= lttng_notification_create(condition
, evaluation
);
131 if (!*notification
) {
136 (*notification
)->owns_elements
= true;
140 lttng_condition_destroy(condition
);
141 lttng_evaluation_destroy(evaluation
);
145 void lttng_notification_destroy(struct lttng_notification
*notification
)
151 if (notification
->owns_elements
) {
152 lttng_condition_destroy(notification
->condition
);
153 lttng_evaluation_destroy(notification
->evaluation
);
158 struct lttng_condition
*lttng_notification_get_condition(
159 struct lttng_notification
*notification
)
161 return notification
? notification
->condition
: NULL
;
164 struct lttng_evaluation
*lttng_notification_get_evaluation(
165 struct lttng_notification
*notification
)
167 return notification
? notification
->evaluation
: NULL
;