Deliverables 3 and 4
[deliverable/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;
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(const char *buf,
90 struct lttng_notification **notification)
91 {
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;
96
97 if (!buf || !notification) {
98 ret = -1;
99 goto end;
100 }
101
102 notification_comm = (struct lttng_notification_comm *) buf;
103 offset += sizeof(*notification_comm);
104
105 /* struct lttng_condition */
106 condition_size = lttng_condition_create_from_buffer(buf + offset,
107 &condition);
108 if (condition_size < 0) {
109 ret = condition_size;
110 goto end;
111 }
112 offset += condition_size;
113
114 /* struct lttng_evaluation */
115 evaluation_size = lttng_evaluation_create_from_buffer(buf + offset,
116 &evaluation);
117 if (evaluation_size < 0) {
118 ret = evaluation_size;
119 goto end;
120 }
121 offset += evaluation_size;
122
123 /* Unexpected size of inner-elements; the buffer is corrupted. */
124 if ((ssize_t) notification_comm->length !=
125 condition_size + evaluation_size) {
126 ret = -1;
127 goto error;
128 }
129
130 *notification = lttng_notification_create(condition, evaluation);
131 if (!*notification) {
132 ret = -1;
133 goto error;
134 }
135 ret = offset;
136 (*notification)->owns_elements = true;
137 end:
138 return ret;
139 error:
140 lttng_condition_destroy(condition);
141 lttng_evaluation_destroy(evaluation);
142 return ret;
143 }
144
145 void lttng_notification_destroy(struct lttng_notification *notification)
146 {
147 if (!notification) {
148 return;
149 }
150
151 if (notification->owns_elements) {
152 lttng_condition_destroy(notification->condition);
153 lttng_evaluation_destroy(notification->evaluation);
154 }
155 free(notification);
156 }
157
158 struct lttng_condition *lttng_notification_get_condition(
159 struct lttng_notification *notification)
160 {
161 return notification ? notification->condition : NULL;
162 }
163
164 struct lttng_evaluation *lttng_notification_get_evaluation(
165 struct lttng_notification *notification)
166 {
167 return notification ? notification->evaluation : NULL;
168 }
This page took 0.034378 seconds and 5 git commands to generate.