common: compile libcommon as C++
[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
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>
52d55cf9 13#include <lttng/trigger/trigger-internal.h>
9e620ea7
JG
14#include <common/payload.h>
15#include <common/payload-view.h>
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
a6bc4ca9 27 notification = (lttng_notification *) zmalloc(sizeof(struct 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;
1e9e2705 43 struct lttng_notification_comm notification_comm = { 0 };
3647288f 44 struct lttng_notification_comm *header;
a58c490f 45
c0a66c84
JG
46 header_offset = payload->buffer.size;
47 ret = lttng_dynamic_buffer_append(&payload->buffer, &notification_comm,
3647288f 48 sizeof(notification_comm));
28cff59f
JG
49 if (ret) {
50 goto end;
51 }
a58c490f 52
c0a66c84 53 size_before_payload = payload->buffer.size;
52d55cf9 54 ret = lttng_trigger_serialize(notification->trigger,
c0a66c84 55 payload);
3647288f 56 if (ret) {
a58c490f
JG
57 goto end;
58 }
a58c490f 59
c0a66c84 60 ret = lttng_evaluation_serialize(notification->evaluation, payload);
3647288f 61 if (ret) {
a58c490f
JG
62 goto end;
63 }
a58c490f 64
3647288f 65 /* Update payload size. */
c0a66c84
JG
66 header = (typeof(header)) (payload->buffer.data + header_offset);
67 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
a58c490f
JG
68end:
69 return ret;
70
71}
72
c0a66c84
JG
73ssize_t lttng_notification_create_from_payload(
74 struct lttng_payload_view *src_view,
a58c490f
JG
75 struct lttng_notification **notification)
76{
f58ae8ad
JG
77 ssize_t ret, notification_size = 0, trigger_size, evaluation_size;
78 struct lttng_trigger *trigger = NULL;
79 struct lttng_evaluation *evaluation = NULL;
3e6e0df2
JG
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));
a58c490f
JG
84
85 if (!src_view || !notification) {
86 ret = -1;
f58ae8ad 87 goto error;
a58c490f
JG
88 }
89
3e6e0df2
JG
90 if (!lttng_payload_view_is_valid(&notification_comm_view)) {
91 /* Payload not large enough to contain the header. */
92 ret = -1;
f58ae8ad 93 goto error;
3e6e0df2
JG
94 }
95
96 notification_comm = (typeof(notification_comm)) notification_comm_view.buffer.data;
a58c490f 97 notification_size += sizeof(*notification_comm);
c0a66c84
JG
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
f58ae8ad 104 trigger_size = lttng_trigger_create_from_payload(
52d55cf9 105 &condition_view, &trigger);
c0a66c84 106 }
a58c490f 107
f58ae8ad
JG
108 if (trigger_size < 0) {
109 ret = trigger_size;
110 goto error;
a58c490f 111 }
c0a66c84 112
f58ae8ad 113 notification_size += trigger_size;
a58c490f 114
c0a66c84
JG
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(
52d55cf9
JG
122 lttng_trigger_get_const_condition(trigger),
123 &evaluation_view, &evaluation);
c0a66c84
JG
124 }
125
a58c490f
JG
126 if (evaluation_size < 0) {
127 ret = evaluation_size;
f58ae8ad 128 goto error;
a58c490f 129 }
c0a66c84 130
a58c490f
JG
131 notification_size += evaluation_size;
132
133 /* Unexpected size of inner-elements; the buffer is corrupted. */
134 if ((ssize_t) notification_comm->length !=
f58ae8ad 135 trigger_size + evaluation_size) {
a58c490f
JG
136 ret = -1;
137 goto error;
138 }
139
52d55cf9 140 *notification = lttng_notification_create(trigger, evaluation);
a58c490f
JG
141 if (!*notification) {
142 ret = -1;
143 goto error;
144 }
f58ae8ad 145
a58c490f 146 ret = notification_size;
a58c490f 147 return ret;
f58ae8ad 148
a58c490f 149error:
52d55cf9 150 lttng_trigger_destroy(trigger);
a58c490f
JG
151 lttng_evaluation_destroy(evaluation);
152 return ret;
153}
154
155void lttng_notification_destroy(struct lttng_notification *notification)
156{
157 if (!notification) {
158 return;
159 }
160
52d55cf9 161 lttng_trigger_destroy(notification->trigger);
9b63a4aa 162 lttng_evaluation_destroy(notification->evaluation);
a58c490f
JG
163 free(notification);
164}
165
166const struct lttng_condition *lttng_notification_get_condition(
167 struct lttng_notification *notification)
168{
52d55cf9 169 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
a58c490f
JG
170}
171
172const struct lttng_evaluation *lttng_notification_get_evaluation(
173 struct lttng_notification *notification)
174{
175 return notification ? notification->evaluation : NULL;
176}
6bec8cb2
JG
177
178const struct lttng_trigger *lttng_notification_get_trigger(
179 struct lttng_notification *notification)
180{
181 return notification ? notification->trigger : NULL;
182}
This page took 0.047603 seconds and 4 git commands to generate.