tests: Use `--no-wait` when destroying sessions in relayd-grouping
[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
28ab034a
JG
8#include <common/payload-view.hpp>
9#include <common/payload.hpp>
10
c9e313bc 11#include <lttng/condition/condition-internal.hpp>
a58c490f 12#include <lttng/condition/condition.h>
28ab034a 13#include <lttng/condition/evaluation-internal.hpp>
a58c490f 14#include <lttng/condition/evaluation.h>
28ab034a 15#include <lttng/notification/notification-internal.hpp>
c9e313bc 16#include <lttng/trigger/trigger-internal.hpp>
a58c490f 17
28ab034a
JG
18struct lttng_notification *lttng_notification_create(struct lttng_trigger *trigger,
19 struct lttng_evaluation *evaluation)
a58c490f 20{
cd9adb8b 21 struct lttng_notification *notification = nullptr;
a58c490f 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,
28ab034a 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 48 header_offset = payload->buffer.size;
28ab034a
JG
49 ret = lttng_dynamic_buffer_append(
50 &payload->buffer, &notification_comm, sizeof(notification_comm));
28cff59f
JG
51 if (ret) {
52 goto end;
53 }
a58c490f 54
c0a66c84 55 size_before_payload = payload->buffer.size;
28ab034a 56 ret = lttng_trigger_serialize(notification->trigger, payload);
3647288f 57 if (ret) {
a58c490f
JG
58 goto end;
59 }
a58c490f 60
c0a66c84 61 ret = lttng_evaluation_serialize(notification->evaluation, payload);
3647288f 62 if (ret) {
a58c490f
JG
63 goto end;
64 }
a58c490f 65
3647288f 66 /* Update payload size. */
c0a66c84
JG
67 header = (typeof(header)) (payload->buffer.data + header_offset);
68 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
a58c490f
JG
69end:
70 return ret;
a58c490f
JG
71}
72
28ab034a
JG
73ssize_t lttng_notification_create_from_payload(struct lttng_payload_view *src_view,
74 struct lttng_notification **notification)
a58c490f 75{
f58ae8ad 76 ssize_t ret, notification_size = 0, trigger_size, evaluation_size;
cd9adb8b
JG
77 struct lttng_trigger *trigger = nullptr;
78 struct lttng_evaluation *evaluation = nullptr;
3e6e0df2
JG
79 const struct lttng_notification_comm *notification_comm;
80 const struct lttng_payload_view notification_comm_view =
28ab034a 81 lttng_payload_view_from_view(src_view, 0, sizeof(*notification_comm));
a58c490f
JG
82
83 if (!src_view || !notification) {
84 ret = -1;
f58ae8ad 85 goto error;
a58c490f
JG
86 }
87
3e6e0df2
JG
88 if (!lttng_payload_view_is_valid(&notification_comm_view)) {
89 /* Payload not large enough to contain the header. */
90 ret = -1;
f58ae8ad 91 goto error;
3e6e0df2
JG
92 }
93
94 notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data;
a58c490f 95 notification_size += sizeof(*notification_comm);
c0a66c84
JG
96 {
97 /* struct lttng_condition */
98 struct lttng_payload_view condition_view =
28ab034a 99 lttng_payload_view_from_view(src_view, notification_size, -1);
c0a66c84 100
28ab034a 101 trigger_size = lttng_trigger_create_from_payload(&condition_view, &trigger);
c0a66c84 102 }
a58c490f 103
f58ae8ad
JG
104 if (trigger_size < 0) {
105 ret = trigger_size;
106 goto error;
a58c490f 107 }
c0a66c84 108
f58ae8ad 109 notification_size += trigger_size;
a58c490f 110
c0a66c84
JG
111 {
112 /* struct lttng_evaluation */
113 struct lttng_payload_view evaluation_view =
28ab034a 114 lttng_payload_view_from_view(src_view, notification_size, -1);
c0a66c84
JG
115
116 evaluation_size = lttng_evaluation_create_from_payload(
28ab034a 117 lttng_trigger_get_const_condition(trigger), &evaluation_view, &evaluation);
c0a66c84
JG
118 }
119
a58c490f
JG
120 if (evaluation_size < 0) {
121 ret = evaluation_size;
f58ae8ad 122 goto error;
a58c490f 123 }
c0a66c84 124
a58c490f
JG
125 notification_size += evaluation_size;
126
127 /* Unexpected size of inner-elements; the buffer is corrupted. */
28ab034a 128 if ((ssize_t) notification_comm->length != trigger_size + evaluation_size) {
a58c490f
JG
129 ret = -1;
130 goto error;
131 }
132
52d55cf9 133 *notification = lttng_notification_create(trigger, evaluation);
a58c490f
JG
134 if (!*notification) {
135 ret = -1;
136 goto error;
137 }
f58ae8ad 138
a58c490f 139 ret = notification_size;
a58c490f 140 return ret;
f58ae8ad 141
a58c490f 142error:
52d55cf9 143 lttng_trigger_destroy(trigger);
a58c490f
JG
144 lttng_evaluation_destroy(evaluation);
145 return ret;
146}
147
148void lttng_notification_destroy(struct lttng_notification *notification)
149{
150 if (!notification) {
151 return;
152 }
153
52d55cf9 154 lttng_trigger_destroy(notification->trigger);
9b63a4aa 155 lttng_evaluation_destroy(notification->evaluation);
a58c490f
JG
156 free(notification);
157}
158
28ab034a
JG
159const struct lttng_condition *
160lttng_notification_get_condition(struct lttng_notification *notification)
a58c490f 161{
cd9adb8b 162 return notification ? lttng_trigger_get_const_condition(notification->trigger) : nullptr;
a58c490f
JG
163}
164
28ab034a
JG
165const struct lttng_evaluation *
166lttng_notification_get_evaluation(struct lttng_notification *notification)
a58c490f 167{
cd9adb8b 168 return notification ? notification->evaluation : nullptr;
a58c490f 169}
6bec8cb2 170
28ab034a
JG
171const struct lttng_condition *
172lttng_notification_get_const_condition(const struct lttng_notification *notification)
c08136a3 173{
cd9adb8b 174 return notification ? lttng_trigger_get_const_condition(notification->trigger) : nullptr;
c08136a3
JG
175}
176
28ab034a
JG
177const struct lttng_evaluation *
178lttng_notification_get_const_evaluation(const struct lttng_notification *notification)
c08136a3 179{
cd9adb8b 180 return notification ? notification->evaluation : nullptr;
c08136a3
JG
181}
182
28ab034a
JG
183const struct lttng_trigger *
184lttng_notification_get_const_trigger(const struct lttng_notification *notification)
c08136a3 185{
cd9adb8b 186 return notification ? notification->trigger : nullptr;
c08136a3
JG
187}
188
28ab034a 189const struct lttng_trigger *lttng_notification_get_trigger(struct lttng_notification *notification)
6bec8cb2 190{
cd9adb8b 191 return notification ? notification->trigger : nullptr;
6bec8cb2 192}
This page took 0.074718 seconds and 4 git commands to generate.