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