c640b9e7535fe78643a7602453b927523af33f6d
[lttng-tools.git] / src / common / notification.c
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 #include <assert.h>
17
18 LTTNG_HIDDEN
19 struct lttng_notification *lttng_notification_create(
20 struct lttng_trigger *trigger,
21 struct lttng_evaluation *evaluation)
22 {
23 struct lttng_notification *notification = NULL;
24
25 if (!trigger || !evaluation) {
26 goto end;
27 }
28
29 notification = zmalloc(sizeof(struct lttng_notification));
30 if (!notification) {
31 goto end;
32 }
33
34 notification->trigger = trigger;
35 notification->evaluation = evaluation;
36 end:
37 return notification;
38 }
39
40 LTTNG_HIDDEN
41 int lttng_notification_serialize(const struct lttng_notification *notification,
42 struct lttng_payload *payload)
43 {
44 int ret;
45 size_t header_offset, size_before_payload;
46 struct lttng_notification_comm notification_comm = { 0 };
47 struct lttng_notification_comm *header;
48
49 header_offset = payload->buffer.size;
50 ret = lttng_dynamic_buffer_append(&payload->buffer, &notification_comm,
51 sizeof(notification_comm));
52 if (ret) {
53 goto end;
54 }
55
56 size_before_payload = payload->buffer.size;
57 ret = lttng_trigger_serialize(notification->trigger,
58 payload);
59 if (ret) {
60 goto end;
61 }
62
63 ret = lttng_evaluation_serialize(notification->evaluation, payload);
64 if (ret) {
65 goto end;
66 }
67
68 /* Update payload size. */
69 header = (typeof(header)) (payload->buffer.data + header_offset);
70 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
71 end:
72 return ret;
73
74 }
75
76 LTTNG_HIDDEN
77 ssize_t lttng_notification_create_from_payload(
78 struct lttng_payload_view *src_view,
79 struct lttng_notification **notification)
80 {
81 ssize_t ret, notification_size = 0, condition_size, evaluation_size;
82 struct lttng_trigger *trigger;
83 struct lttng_evaluation *evaluation;
84 const struct lttng_notification_comm *notification_comm;
85 const struct lttng_payload_view notification_comm_view =
86 lttng_payload_view_from_view(
87 src_view, 0, sizeof(*notification_comm));
88
89 if (!src_view || !notification) {
90 ret = -1;
91 goto end;
92 }
93
94 if (!lttng_payload_view_is_valid(&notification_comm_view)) {
95 /* Payload not large enough to contain the header. */
96 ret = -1;
97 goto end;
98 }
99
100 notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data;
101 notification_size += sizeof(*notification_comm);
102 {
103 /* struct lttng_condition */
104 struct lttng_payload_view condition_view =
105 lttng_payload_view_from_view(src_view,
106 notification_size, -1);
107
108 condition_size = lttng_trigger_create_from_payload(
109 &condition_view, &trigger);
110 }
111
112 if (condition_size < 0) {
113 ret = condition_size;
114 goto end;
115 }
116
117 notification_size += condition_size;
118
119 {
120 /* struct lttng_evaluation */
121 struct lttng_payload_view evaluation_view =
122 lttng_payload_view_from_view(src_view,
123 notification_size, -1);
124
125 evaluation_size = lttng_evaluation_create_from_payload(
126 lttng_trigger_get_const_condition(trigger),
127 &evaluation_view, &evaluation);
128 }
129
130 if (evaluation_size < 0) {
131 ret = evaluation_size;
132 goto end;
133 }
134
135 notification_size += evaluation_size;
136
137 /* Unexpected size of inner-elements; the buffer is corrupted. */
138 if ((ssize_t) notification_comm->length !=
139 condition_size + evaluation_size) {
140 ret = -1;
141 goto error;
142 }
143
144 *notification = lttng_notification_create(trigger, evaluation);
145 if (!*notification) {
146 ret = -1;
147 goto error;
148 }
149 ret = notification_size;
150 end:
151 return ret;
152 error:
153 lttng_trigger_destroy(trigger);
154 lttng_evaluation_destroy(evaluation);
155 return ret;
156 }
157
158 void lttng_notification_destroy(struct lttng_notification *notification)
159 {
160 if (!notification) {
161 return;
162 }
163
164 lttng_trigger_destroy(notification->trigger);
165 lttng_evaluation_destroy(notification->evaluation);
166 free(notification);
167 }
168
169 const struct lttng_condition *lttng_notification_get_condition(
170 struct lttng_notification *notification)
171 {
172 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
173 }
174
175 const struct lttng_evaluation *lttng_notification_get_evaluation(
176 struct lttng_notification *notification)
177 {
178 return notification ? notification->evaluation : NULL;
179 }
This page took 0.032006 seconds and 3 git commands to generate.