lttng-create(1): specify that `--shm-path` only applies to UST channels
[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{
f58ae8ad
JG
81 ssize_t ret, notification_size = 0, trigger_size, evaluation_size;
82 struct lttng_trigger *trigger = NULL;
83 struct lttng_evaluation *evaluation = NULL;
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;
f58ae8ad 91 goto error;
a58c490f
JG
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;
f58ae8ad 97 goto error;
3e6e0df2
JG
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
f58ae8ad 108 trigger_size = lttng_trigger_create_from_payload(
52d55cf9 109 &condition_view, &trigger);
c0a66c84 110 }
a58c490f 111
f58ae8ad
JG
112 if (trigger_size < 0) {
113 ret = trigger_size;
114 goto error;
a58c490f 115 }
c0a66c84 116
f58ae8ad 117 notification_size += trigger_size;
a58c490f 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;
f58ae8ad 132 goto error;
a58c490f 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 !=
f58ae8ad 139 trigger_size + evaluation_size) {
a58c490f
JG
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 }
f58ae8ad 149
a58c490f 150 ret = notification_size;
a58c490f 151 return ret;
f58ae8ad 152
a58c490f 153error:
52d55cf9 154 lttng_trigger_destroy(trigger);
a58c490f
JG
155 lttng_evaluation_destroy(evaluation);
156 return ret;
157}
158
159void lttng_notification_destroy(struct lttng_notification *notification)
160{
161 if (!notification) {
162 return;
163 }
164
52d55cf9 165 lttng_trigger_destroy(notification->trigger);
9b63a4aa 166 lttng_evaluation_destroy(notification->evaluation);
a58c490f
JG
167 free(notification);
168}
169
170const struct lttng_condition *lttng_notification_get_condition(
171 struct lttng_notification *notification)
172{
52d55cf9 173 return notification ? lttng_trigger_get_const_condition(notification->trigger) : NULL;
a58c490f
JG
174}
175
176const struct lttng_evaluation *lttng_notification_get_evaluation(
177 struct lttng_notification *notification)
178{
179 return notification ? notification->evaluation : NULL;
180}
6bec8cb2
JG
181
182const struct lttng_trigger *lttng_notification_get_trigger(
183 struct lttng_notification *notification)
184{
185 return notification ? notification->trigger : NULL;
186}
This page took 0.050845 seconds and 4 git commands to generate.