Commit | Line | Data |
---|---|---|
a58c490f JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * for more details. | |
12 | * | |
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 | |
16 | */ | |
17 | ||
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> | |
23 | #include <assert.h> | |
24 | ||
25 | LTTNG_HIDDEN | |
26 | struct lttng_notification *lttng_notification_create( | |
27 | struct lttng_condition *condition, | |
28 | struct lttng_evaluation *evaluation) | |
29 | { | |
30 | struct lttng_notification *notification = NULL; | |
31 | ||
32 | if (!condition || !evaluation) { | |
33 | goto end; | |
34 | } | |
35 | ||
36 | notification = zmalloc(sizeof(struct lttng_notification)); | |
37 | if (!notification) { | |
38 | goto end; | |
39 | } | |
40 | ||
41 | notification->condition = condition; | |
42 | notification->evaluation = evaluation; | |
43 | notification->owns_elements = false; | |
44 | end: | |
45 | return notification; | |
46 | } | |
47 | ||
48 | LTTNG_HIDDEN | |
3647288f JG |
49 | int lttng_notification_serialize(struct lttng_notification *notification, |
50 | struct lttng_dynamic_buffer *buf) | |
a58c490f | 51 | { |
3647288f JG |
52 | int ret; |
53 | size_t header_offset, size_before_payload; | |
1e9e2705 | 54 | struct lttng_notification_comm notification_comm = { 0 }; |
3647288f | 55 | struct lttng_notification_comm *header; |
a58c490f | 56 | |
3647288f JG |
57 | header_offset = buf->size; |
58 | ret = lttng_dynamic_buffer_append(buf, ¬ification_comm, | |
59 | sizeof(notification_comm)); | |
a58c490f | 60 | |
3647288f JG |
61 | size_before_payload = buf->size; |
62 | ret = lttng_condition_serialize(notification->condition, | |
63 | buf); | |
64 | if (ret) { | |
a58c490f JG |
65 | goto end; |
66 | } | |
a58c490f | 67 | |
3647288f JG |
68 | ret = lttng_evaluation_serialize(notification->evaluation, buf); |
69 | if (ret) { | |
a58c490f JG |
70 | goto end; |
71 | } | |
a58c490f | 72 | |
3647288f JG |
73 | /* Update payload size. */ |
74 | header = (struct lttng_notification_comm *) ((char *) buf->data + header_offset); | |
75 | header->length = (uint32_t) (buf->size - size_before_payload); | |
a58c490f JG |
76 | end: |
77 | return ret; | |
78 | ||
79 | } | |
80 | ||
81 | LTTNG_HIDDEN | |
82 | ssize_t lttng_notification_create_from_buffer( | |
83 | const struct lttng_buffer_view *src_view, | |
84 | struct lttng_notification **notification) | |
85 | { | |
86 | ssize_t ret, notification_size = 0, condition_size, evaluation_size; | |
87 | const struct lttng_notification_comm *notification_comm; | |
88 | struct lttng_condition *condition; | |
89 | struct lttng_evaluation *evaluation; | |
90 | struct lttng_buffer_view condition_view; | |
91 | struct lttng_buffer_view evaluation_view; | |
92 | ||
93 | if (!src_view || !notification) { | |
94 | ret = -1; | |
95 | goto end; | |
96 | } | |
97 | ||
98 | notification_comm = | |
99 | (const struct lttng_notification_comm *) src_view->data; | |
100 | notification_size += sizeof(*notification_comm); | |
101 | ||
102 | /* struct lttng_condition */ | |
103 | condition_view = lttng_buffer_view_from_view(src_view, | |
104 | sizeof(*notification_comm), -1); | |
105 | condition_size = lttng_condition_create_from_buffer(&condition_view, | |
106 | &condition); | |
107 | if (condition_size < 0) { | |
108 | ret = condition_size; | |
109 | goto end; | |
110 | } | |
111 | notification_size += condition_size; | |
112 | ||
113 | /* struct lttng_evaluation */ | |
114 | evaluation_view = lttng_buffer_view_from_view(&condition_view, | |
115 | condition_size, -1); | |
116 | evaluation_size = lttng_evaluation_create_from_buffer(&evaluation_view, | |
117 | &evaluation); | |
118 | if (evaluation_size < 0) { | |
119 | ret = evaluation_size; | |
120 | goto end; | |
121 | } | |
122 | notification_size += evaluation_size; | |
123 | ||
124 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
125 | if ((ssize_t) notification_comm->length != | |
126 | condition_size + evaluation_size) { | |
127 | ret = -1; | |
128 | goto error; | |
129 | } | |
130 | ||
131 | *notification = lttng_notification_create(condition, evaluation); | |
132 | if (!*notification) { | |
133 | ret = -1; | |
134 | goto error; | |
135 | } | |
136 | ret = notification_size; | |
137 | (*notification)->owns_elements = true; | |
138 | end: | |
139 | return ret; | |
140 | error: | |
141 | lttng_condition_destroy(condition); | |
142 | lttng_evaluation_destroy(evaluation); | |
143 | return ret; | |
144 | } | |
145 | ||
146 | void lttng_notification_destroy(struct lttng_notification *notification) | |
147 | { | |
148 | if (!notification) { | |
149 | return; | |
150 | } | |
151 | ||
152 | if (notification->owns_elements) { | |
153 | lttng_condition_destroy(notification->condition); | |
154 | lttng_evaluation_destroy(notification->evaluation); | |
155 | } | |
156 | free(notification); | |
157 | } | |
158 | ||
159 | const struct lttng_condition *lttng_notification_get_condition( | |
160 | struct lttng_notification *notification) | |
161 | { | |
162 | return notification ? notification->condition : NULL; | |
163 | } | |
164 | ||
165 | const struct lttng_evaluation *lttng_notification_get_evaluation( | |
166 | struct lttng_notification *notification) | |
167 | { | |
168 | return notification ? notification->evaluation : NULL; | |
169 | } |