Build fix: missing initializer for member 'payload'
[lttng-tools.git] / src / common / notification.cpp
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/notification/notification-internal.hpp>
9 #include <lttng/condition/condition-internal.hpp>
10 #include <lttng/condition/evaluation-internal.hpp>
11 #include <lttng/condition/condition.h>
12 #include <lttng/condition/evaluation.h>
13 #include <lttng/trigger/trigger-internal.hpp>
14 #include <common/payload.hpp>
15 #include <common/payload-view.hpp>
16
17 struct lttng_notification *lttng_notification_create(
18 struct lttng_trigger *trigger,
19 struct lttng_evaluation *evaluation)
20 {
21 struct lttng_notification *notification = NULL;
22
23 if (!trigger || !evaluation) {
24 goto end;
25 }
26
27 notification = zmalloc<lttng_notification>();
28 if (!notification) {
29 goto end;
30 }
31
32 notification->trigger = trigger;
33 notification->evaluation = evaluation;
34 end:
35 return notification;
36 }
37
38 int lttng_notification_serialize(const struct lttng_notification *notification,
39 struct lttng_payload *payload)
40 {
41 int ret;
42 size_t header_offset, size_before_payload;
43 struct lttng_notification_comm notification_comm;
44 struct lttng_notification_comm *header;
45
46 notification_comm.length = 0;
47
48 header_offset = payload->buffer.size;
49 ret = lttng_dynamic_buffer_append(&payload->buffer, &notification_comm,
50 sizeof(notification_comm));
51 if (ret) {
52 goto end;
53 }
54
55 size_before_payload = payload->buffer.size;
56 ret = lttng_trigger_serialize(notification->trigger,
57 payload);
58 if (ret) {
59 goto end;
60 }
61
62 ret = lttng_evaluation_serialize(notification->evaluation, payload);
63 if (ret) {
64 goto end;
65 }
66
67 /* Update payload size. */
68 header = (typeof(header)) (payload->buffer.data + header_offset);
69 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
70 end:
71 return ret;
72
73 }
74
75 ssize_t lttng_notification_create_from_payload(
76 struct lttng_payload_view *src_view,
77 struct lttng_notification **notification)
78 {
79 ssize_t ret, notification_size = 0, trigger_size, evaluation_size;
80 struct lttng_trigger *trigger = NULL;
81 struct lttng_evaluation *evaluation = NULL;
82 const struct lttng_notification_comm *notification_comm;
83 const struct lttng_payload_view notification_comm_view =
84 lttng_payload_view_from_view(
85 src_view, 0, sizeof(*notification_comm));
86
87 if (!src_view || !notification) {
88 ret = -1;
89 goto error;
90 }
91
92 if (!lttng_payload_view_is_valid(&notification_comm_view)) {
93 /* Payload not large enough to contain the header. */
94 ret = -1;
95 goto error;
96 }
97
98 notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data;
99 notification_size += sizeof(*notification_comm);
100 {
101 /* struct lttng_condition */
102 struct lttng_payload_view condition_view =
103 lttng_payload_view_from_view(src_view,
104 notification_size, -1);
105
106 trigger_size = lttng_trigger_create_from_payload(
107 &condition_view, &trigger);
108 }
109
110 if (trigger_size < 0) {
111 ret = trigger_size;
112 goto error;
113 }
114
115 notification_size += trigger_size;
116
117 {
118 /* struct lttng_evaluation */
119 struct lttng_payload_view evaluation_view =
120 lttng_payload_view_from_view(src_view,
121 notification_size, -1);
122
123 evaluation_size = lttng_evaluation_create_from_payload(
124 lttng_trigger_get_const_condition(trigger),
125 &evaluation_view, &evaluation);
126 }
127
128 if (evaluation_size < 0) {
129 ret = evaluation_size;
130 goto error;
131 }
132
133 notification_size += evaluation_size;
134
135 /* Unexpected size of inner-elements; the buffer is corrupted. */
136 if ((ssize_t) notification_comm->length !=
137 trigger_size + evaluation_size) {
138 ret = -1;
139 goto error;
140 }
141
142 *notification = lttng_notification_create(trigger, evaluation);
143 if (!*notification) {
144 ret = -1;
145 goto error;
146 }
147
148 ret = notification_size;
149 return ret;
150
151 error:
152 lttng_trigger_destroy(trigger);
153 lttng_evaluation_destroy(evaluation);
154 return ret;
155 }
156
157 void lttng_notification_destroy(struct lttng_notification *notification)
158 {
159 if (!notification) {
160 return;
161 }
162
163 lttng_trigger_destroy(notification->trigger);
164 lttng_evaluation_destroy(notification->evaluation);
165 free(notification);
166 }
167
168 const struct lttng_condition *lttng_notification_get_condition(
169 struct lttng_notification *notification)
170 {
171 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
172 }
173
174 const struct lttng_evaluation *lttng_notification_get_evaluation(
175 struct lttng_notification *notification)
176 {
177 return notification ? notification->evaluation : NULL;
178 }
179
180 const struct lttng_condition *lttng_notification_get_const_condition(
181 const struct lttng_notification *notification)
182 {
183 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
184 }
185
186 const struct lttng_evaluation *lttng_notification_get_const_evaluation(
187 const struct lttng_notification *notification)
188 {
189 return notification ? notification->evaluation : NULL;
190 }
191
192 const struct lttng_trigger *lttng_notification_get_const_trigger(
193 const struct lttng_notification *notification)
194 {
195 return notification ? notification->trigger : NULL;
196 }
197
198 const struct lttng_trigger *lttng_notification_get_trigger(
199 struct lttng_notification *notification)
200 {
201 return notification ? notification->trigger : NULL;
202 }
This page took 0.032694 seconds and 4 git commands to generate.