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 ssize_t
lttng_notification_serialize(struct lttng_notification
*notification
,
51 ssize_t ret
, condition_size
, evaluation_size
, offset
= 0;
52 struct lttng_notification_comm notification_comm
;
59 offset
+= sizeof(notification_comm
);
60 condition_size
= lttng_condition_serialize(notification
->condition
,
61 buf
? (buf
+ offset
) : NULL
);
62 if (condition_size
< 0) {
66 offset
+= condition_size
;
68 evaluation_size
= lttng_evaluation_serialize(notification
->evaluation
,
69 buf
? (buf
+ offset
) : NULL
);
70 if (evaluation_size
< 0) {
71 ret
= evaluation_size
;
74 offset
+= evaluation_size
;
77 notification_comm
.length
=
78 (uint32_t) (condition_size
+ evaluation_size
);
79 memcpy(buf
, ¬ification_comm
, sizeof(notification_comm
));
88 ssize_t
lttng_notification_create_from_buffer(const char *buf
,
89 struct lttng_notification
**notification
)
91 ssize_t ret
, offset
= 0, condition_size
, evaluation_size
;
92 struct lttng_notification_comm
*notification_comm
;
93 struct lttng_condition
*condition
;
94 struct lttng_evaluation
*evaluation
;
96 if (!buf
|| !notification
) {
101 notification_comm
= (struct lttng_notification_comm
*) buf
;
102 offset
+= sizeof(*notification_comm
);
104 /* struct lttng_condition */
105 condition_size
= lttng_condition_create_from_buffer(buf
+ offset
,
107 if (condition_size
< 0) {
108 ret
= condition_size
;
111 offset
+= condition_size
;
113 /* struct lttng_evaluation */
114 evaluation_size
= lttng_evaluation_create_from_buffer(buf
+ offset
,
116 if (evaluation_size
< 0) {
117 ret
= evaluation_size
;
120 offset
+= evaluation_size
;
122 /* Unexpected size of inner-elements; the buffer is corrupted. */
123 if ((ssize_t
) notification_comm
->length
!=
124 condition_size
+ evaluation_size
) {
129 *notification
= lttng_notification_create(condition
, evaluation
);
130 if (!*notification
) {
137 lttng_condition_destroy(condition
);
138 lttng_evaluation_destroy(evaluation
);
142 void lttng_notification_destroy(struct lttng_notification
*notification
)
148 lttng_condition_destroy(notification
->condition
);
149 lttng_evaluation_destroy(notification
->evaluation
);
153 struct lttng_condition
*lttng_notification_get_condition(
154 struct lttng_notification
*notification
)
156 return notification
? notification
->condition
: NULL
;
159 struct lttng_evaluation
*lttng_notification_get_evaluation(
160 struct lttng_notification
*notification
)
162 return notification
? notification
->evaluation
: NULL
;