event-rule: Normalize pattern for syscall and tracepoint
[lttng-tools.git] / src / common / notification.c
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
JG
16#include <assert.h>
17
18LTTNG_HIDDEN
19struct lttng_notification *lttng_notification_create(
52d55cf9 20 struct lttng_trigger *trigger,
a58c490f
JG
21 struct lttng_evaluation *evaluation)
22{
23 struct lttng_notification *notification = NULL;
24
52d55cf9 25 if (!trigger || !evaluation) {
a58c490f
JG
26 goto end;
27 }
28
29 notification = zmalloc(sizeof(struct lttng_notification));
30 if (!notification) {
31 goto end;
32 }
33
52d55cf9 34 notification->trigger = trigger;
a58c490f 35 notification->evaluation = evaluation;
a58c490f
JG
36end:
37 return notification;
38}
39
40LTTNG_HIDDEN
9b63a4aa 41int lttng_notification_serialize(const struct lttng_notification *notification,
c0a66c84 42 struct lttng_payload *payload)
a58c490f 43{
3647288f
JG
44 int ret;
45 size_t header_offset, size_before_payload;
1e9e2705 46 struct lttng_notification_comm notification_comm = { 0 };
3647288f 47 struct lttng_notification_comm *header;
a58c490f 48
c0a66c84
JG
49 header_offset = payload->buffer.size;
50 ret = lttng_dynamic_buffer_append(&payload->buffer, &notification_comm,
3647288f 51 sizeof(notification_comm));
28cff59f
JG
52 if (ret) {
53 goto end;
54 }
a58c490f 55
c0a66c84 56 size_before_payload = payload->buffer.size;
52d55cf9 57 ret = lttng_trigger_serialize(notification->trigger,
c0a66c84 58 payload);
3647288f 59 if (ret) {
a58c490f
JG
60 goto end;
61 }
a58c490f 62
c0a66c84 63 ret = lttng_evaluation_serialize(notification->evaluation, payload);
3647288f 64 if (ret) {
a58c490f
JG
65 goto end;
66 }
a58c490f 67
3647288f 68 /* Update payload size. */
c0a66c84
JG
69 header = (typeof(header)) (payload->buffer.data + header_offset);
70 header->length = (uint32_t) (payload->buffer.size - size_before_payload);
a58c490f
JG
71end:
72 return ret;
73
74}
75
76LTTNG_HIDDEN
c0a66c84
JG
77ssize_t lttng_notification_create_from_payload(
78 struct lttng_payload_view *src_view,
a58c490f
JG
79 struct lttng_notification **notification)
80{
81 ssize_t ret, notification_size = 0, condition_size, evaluation_size;
52d55cf9 82 struct lttng_trigger *trigger;
a58c490f 83 struct lttng_evaluation *evaluation;
3e6e0df2
JG
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));
a58c490f
JG
88
89 if (!src_view || !notification) {
90 ret = -1;
91 goto end;
92 }
93
3e6e0df2
JG
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;
a58c490f 101 notification_size += sizeof(*notification_comm);
c0a66c84
JG
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
52d55cf9
JG
108 condition_size = lttng_trigger_create_from_payload(
109 &condition_view, &trigger);
c0a66c84 110 }
a58c490f 111
a58c490f
JG
112 if (condition_size < 0) {
113 ret = condition_size;
114 goto end;
115 }
c0a66c84 116
a58c490f
JG
117 notification_size += condition_size;
118
c0a66c84
JG
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(
52d55cf9
JG
126 lttng_trigger_get_const_condition(trigger),
127 &evaluation_view, &evaluation);
c0a66c84
JG
128 }
129
a58c490f
JG
130 if (evaluation_size < 0) {
131 ret = evaluation_size;
132 goto end;
133 }
c0a66c84 134
a58c490f
JG
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
52d55cf9 144 *notification = lttng_notification_create(trigger, evaluation);
a58c490f
JG
145 if (!*notification) {
146 ret = -1;
147 goto error;
148 }
149 ret = notification_size;
a58c490f
JG
150end:
151 return ret;
152error:
52d55cf9 153 lttng_trigger_destroy(trigger);
a58c490f
JG
154 lttng_evaluation_destroy(evaluation);
155 return ret;
156}
157
158void lttng_notification_destroy(struct lttng_notification *notification)
159{
160 if (!notification) {
161 return;
162 }
163
52d55cf9 164 lttng_trigger_destroy(notification->trigger);
9b63a4aa 165 lttng_evaluation_destroy(notification->evaluation);
a58c490f
JG
166 free(notification);
167}
168
169const struct lttng_condition *lttng_notification_get_condition(
170 struct lttng_notification *notification)
171{
52d55cf9 172 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
a58c490f
JG
173}
174
175const struct lttng_evaluation *lttng_notification_get_evaluation(
176 struct lttng_notification *notification)
177{
178 return notification ? notification->evaluation : NULL;
179}
6bec8cb2
JG
180
181const struct lttng_trigger *lttng_notification_get_trigger(
182 struct lttng_notification *notification)
183{
184 return notification ? notification->trigger : NULL;
185}
This page took 0.044472 seconds and 4 git commands to generate.