785af6816f4453e5ed2b06e5845f3eea1f5e4e6c
[lttng-tools.git] / src / common / notification.c
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
49 ssize_t lttng_notification_serialize(struct lttng_notification *notification,
50 char *buf)
51 {
52 ssize_t ret, condition_size, evaluation_size, offset = 0;
53 struct lttng_notification_comm notification_comm = { 0 };
54
55 if (!notification) {
56 ret = -1;
57 goto end;
58 }
59
60 offset += sizeof(notification_comm);
61 condition_size = lttng_condition_serialize(notification->condition,
62 buf ? (buf + offset) : NULL);
63 if (condition_size < 0) {
64 ret = condition_size;
65 goto end;
66 }
67 offset += condition_size;
68
69 evaluation_size = lttng_evaluation_serialize(notification->evaluation,
70 buf ? (buf + offset) : NULL);
71 if (evaluation_size < 0) {
72 ret = evaluation_size;
73 goto end;
74 }
75 offset += evaluation_size;
76
77 if (buf) {
78 notification_comm.length =
79 (uint32_t) (condition_size + evaluation_size);
80 memcpy(buf, &notification_comm, sizeof(notification_comm));
81 }
82 ret = offset;
83 end:
84 return ret;
85
86 }
87
88 LTTNG_HIDDEN
89 ssize_t lttng_notification_create_from_buffer(
90 const struct lttng_buffer_view *src_view,
91 struct lttng_notification **notification)
92 {
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;
99
100 if (!src_view || !notification) {
101 ret = -1;
102 goto end;
103 }
104
105 notification_comm =
106 (const struct lttng_notification_comm *) src_view->data;
107 notification_size += sizeof(*notification_comm);
108
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,
113 &condition);
114 if (condition_size < 0) {
115 ret = condition_size;
116 goto end;
117 }
118 notification_size += condition_size;
119
120 /* struct lttng_evaluation */
121 evaluation_view = lttng_buffer_view_from_view(&condition_view,
122 condition_size, -1);
123 evaluation_size = lttng_evaluation_create_from_buffer(&evaluation_view,
124 &evaluation);
125 if (evaluation_size < 0) {
126 ret = evaluation_size;
127 goto end;
128 }
129 notification_size += evaluation_size;
130
131 /* Unexpected size of inner-elements; the buffer is corrupted. */
132 if ((ssize_t) notification_comm->length !=
133 condition_size + evaluation_size) {
134 ret = -1;
135 goto error;
136 }
137
138 *notification = lttng_notification_create(condition, evaluation);
139 if (!*notification) {
140 ret = -1;
141 goto error;
142 }
143 ret = notification_size;
144 (*notification)->owns_elements = true;
145 end:
146 return ret;
147 error:
148 lttng_condition_destroy(condition);
149 lttng_evaluation_destroy(evaluation);
150 return ret;
151 }
152
153 void lttng_notification_destroy(struct lttng_notification *notification)
154 {
155 if (!notification) {
156 return;
157 }
158
159 if (notification->owns_elements) {
160 lttng_condition_destroy(notification->condition);
161 lttng_evaluation_destroy(notification->evaluation);
162 }
163 free(notification);
164 }
165
166 const struct lttng_condition *lttng_notification_get_condition(
167 struct lttng_notification *notification)
168 {
169 return notification ? notification->condition : NULL;
170 }
171
172 const struct lttng_evaluation *lttng_notification_get_evaluation(
173 struct lttng_notification *notification)
174 {
175 return notification ? notification->evaluation : NULL;
176 }
This page took 0.048506 seconds and 4 git commands to generate.