989c20ade5e1f126ffcf2609e7f0eea21e2f4028
[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.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/condition/evaluation-internal.h>
11 #include <lttng/condition/condition.h>
12 #include <lttng/condition/evaluation.h>
13 #include <lttng/trigger/trigger-internal.h>
14 #include <common/payload.h>
15 #include <common/payload-view.h>
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 = (lttng_notification *) zmalloc(sizeof(struct 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 = { 0 };
44 struct lttng_notification_comm *header;
45
46 header_offset = payload->buffer.size;
47 ret = lttng_dynamic_buffer_append(&payload->buffer, &notification_comm,
48 sizeof(notification_comm));
49 if (ret) {
50 goto end;
51 }
52
53 size_before_payload = payload->buffer.size;
54 ret = lttng_trigger_serialize(notification->trigger,
55 payload);
56 if (ret) {
57 goto end;
58 }
59
60 ret = lttng_evaluation_serialize(notification->evaluation, payload);
61 if (ret) {
62 goto end;
63 }
64
65 /* Update payload size. */
66 header = (typeof(header)) (payload->buffer.data + header_offset);
67 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
68 end:
69 return ret;
70
71 }
72
73 ssize_t lttng_notification_create_from_payload(
74 struct lttng_payload_view *src_view,
75 struct lttng_notification **notification)
76 {
77 ssize_t ret, notification_size = 0, trigger_size, evaluation_size;
78 struct lttng_trigger *trigger = NULL;
79 struct lttng_evaluation *evaluation = NULL;
80 const struct lttng_notification_comm *notification_comm;
81 const struct lttng_payload_view notification_comm_view =
82 lttng_payload_view_from_view(
83 src_view, 0, sizeof(*notification_comm));
84
85 if (!src_view || !notification) {
86 ret = -1;
87 goto error;
88 }
89
90 if (!lttng_payload_view_is_valid(&notification_comm_view)) {
91 /* Payload not large enough to contain the header. */
92 ret = -1;
93 goto error;
94 }
95
96 notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data;
97 notification_size += sizeof(*notification_comm);
98 {
99 /* struct lttng_condition */
100 struct lttng_payload_view condition_view =
101 lttng_payload_view_from_view(src_view,
102 notification_size, -1);
103
104 trigger_size = lttng_trigger_create_from_payload(
105 &condition_view, &trigger);
106 }
107
108 if (trigger_size < 0) {
109 ret = trigger_size;
110 goto error;
111 }
112
113 notification_size += trigger_size;
114
115 {
116 /* struct lttng_evaluation */
117 struct lttng_payload_view evaluation_view =
118 lttng_payload_view_from_view(src_view,
119 notification_size, -1);
120
121 evaluation_size = lttng_evaluation_create_from_payload(
122 lttng_trigger_get_const_condition(trigger),
123 &evaluation_view, &evaluation);
124 }
125
126 if (evaluation_size < 0) {
127 ret = evaluation_size;
128 goto error;
129 }
130
131 notification_size += evaluation_size;
132
133 /* Unexpected size of inner-elements; the buffer is corrupted. */
134 if ((ssize_t) notification_comm->length !=
135 trigger_size + evaluation_size) {
136 ret = -1;
137 goto error;
138 }
139
140 *notification = lttng_notification_create(trigger, evaluation);
141 if (!*notification) {
142 ret = -1;
143 goto error;
144 }
145
146 ret = notification_size;
147 return ret;
148
149 error:
150 lttng_trigger_destroy(trigger);
151 lttng_evaluation_destroy(evaluation);
152 return ret;
153 }
154
155 void lttng_notification_destroy(struct lttng_notification *notification)
156 {
157 if (!notification) {
158 return;
159 }
160
161 lttng_trigger_destroy(notification->trigger);
162 lttng_evaluation_destroy(notification->evaluation);
163 free(notification);
164 }
165
166 const struct lttng_condition *lttng_notification_get_condition(
167 struct lttng_notification *notification)
168 {
169 return notification ? lttng_trigger_get_const_condition(notification->trigger) : 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 }
177
178 const struct lttng_trigger *lttng_notification_get_trigger(
179 struct lttng_notification *notification)
180 {
181 return notification ? notification->trigger : NULL;
182 }
This page took 0.032912 seconds and 3 git commands to generate.