Commit | Line | Data |
---|---|---|
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> | |
c0a66c84 JG |
13 | #include <common/sessiond-comm/payload.h> |
14 | #include <common/sessiond-comm/payload-view.h> | |
a58c490f JG |
15 | #include <assert.h> |
16 | ||
17 | LTTNG_HIDDEN | |
18 | struct lttng_notification *lttng_notification_create( | |
19 | struct lttng_condition *condition, | |
20 | struct lttng_evaluation *evaluation) | |
21 | { | |
22 | struct lttng_notification *notification = NULL; | |
23 | ||
24 | if (!condition || !evaluation) { | |
25 | goto end; | |
26 | } | |
27 | ||
28 | notification = zmalloc(sizeof(struct lttng_notification)); | |
29 | if (!notification) { | |
30 | goto end; | |
31 | } | |
32 | ||
33 | notification->condition = condition; | |
34 | notification->evaluation = evaluation; | |
a58c490f JG |
35 | end: |
36 | return notification; | |
37 | } | |
38 | ||
39 | LTTNG_HIDDEN | |
9b63a4aa | 40 | int lttng_notification_serialize(const struct lttng_notification *notification, |
c0a66c84 | 41 | struct lttng_payload *payload) |
a58c490f | 42 | { |
3647288f JG |
43 | int ret; |
44 | size_t header_offset, size_before_payload; | |
1e9e2705 | 45 | struct lttng_notification_comm notification_comm = { 0 }; |
3647288f | 46 | struct lttng_notification_comm *header; |
a58c490f | 47 | |
c0a66c84 JG |
48 | header_offset = payload->buffer.size; |
49 | ret = lttng_dynamic_buffer_append(&payload->buffer, ¬ification_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; |
3647288f | 56 | ret = lttng_condition_serialize(notification->condition, |
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 |
70 | end: |
71 | return ret; | |
72 | ||
73 | } | |
74 | ||
75 | LTTNG_HIDDEN | |
c0a66c84 JG |
76 | ssize_t lttng_notification_create_from_payload( |
77 | struct lttng_payload_view *src_view, | |
a58c490f JG |
78 | struct lttng_notification **notification) |
79 | { | |
80 | ssize_t ret, notification_size = 0, condition_size, evaluation_size; | |
81 | const struct lttng_notification_comm *notification_comm; | |
82 | struct lttng_condition *condition; | |
83 | struct lttng_evaluation *evaluation; | |
a58c490f JG |
84 | |
85 | if (!src_view || !notification) { | |
86 | ret = -1; | |
87 | goto end; | |
88 | } | |
89 | ||
c0a66c84 | 90 | notification_comm = (typeof(notification_comm)) src_view->buffer.data; |
a58c490f | 91 | notification_size += sizeof(*notification_comm); |
c0a66c84 JG |
92 | { |
93 | /* struct lttng_condition */ | |
94 | struct lttng_payload_view condition_view = | |
95 | lttng_payload_view_from_view(src_view, | |
96 | notification_size, -1); | |
97 | ||
98 | condition_size = lttng_condition_create_from_payload( | |
99 | &condition_view, &condition); | |
100 | } | |
a58c490f | 101 | |
a58c490f JG |
102 | if (condition_size < 0) { |
103 | ret = condition_size; | |
104 | goto end; | |
105 | } | |
c0a66c84 | 106 | |
a58c490f JG |
107 | notification_size += condition_size; |
108 | ||
c0a66c84 JG |
109 | { |
110 | /* struct lttng_evaluation */ | |
111 | struct lttng_payload_view evaluation_view = | |
112 | lttng_payload_view_from_view(src_view, | |
113 | notification_size, -1); | |
114 | ||
115 | evaluation_size = lttng_evaluation_create_from_payload( | |
116 | &evaluation_view, &evaluation); | |
117 | } | |
118 | ||
a58c490f JG |
119 | if (evaluation_size < 0) { |
120 | ret = evaluation_size; | |
121 | goto end; | |
122 | } | |
c0a66c84 | 123 | |
a58c490f JG |
124 | notification_size += evaluation_size; |
125 | ||
126 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
127 | if ((ssize_t) notification_comm->length != | |
128 | condition_size + evaluation_size) { | |
129 | ret = -1; | |
130 | goto error; | |
131 | } | |
132 | ||
133 | *notification = lttng_notification_create(condition, evaluation); | |
134 | if (!*notification) { | |
135 | ret = -1; | |
136 | goto error; | |
137 | } | |
138 | ret = notification_size; | |
a58c490f JG |
139 | end: |
140 | return ret; | |
141 | error: | |
142 | lttng_condition_destroy(condition); | |
143 | lttng_evaluation_destroy(evaluation); | |
144 | return ret; | |
145 | } | |
146 | ||
147 | void lttng_notification_destroy(struct lttng_notification *notification) | |
148 | { | |
149 | if (!notification) { | |
150 | return; | |
151 | } | |
152 | ||
9b63a4aa JG |
153 | lttng_condition_destroy(notification->condition); |
154 | lttng_evaluation_destroy(notification->evaluation); | |
a58c490f JG |
155 | free(notification); |
156 | } | |
157 | ||
158 | const struct lttng_condition *lttng_notification_get_condition( | |
159 | struct lttng_notification *notification) | |
160 | { | |
161 | return notification ? notification->condition : NULL; | |
162 | } | |
163 | ||
164 | const struct lttng_evaluation *lttng_notification_get_evaluation( | |
165 | struct lttng_notification *notification) | |
166 | { | |
167 | return notification ? notification->evaluation : NULL; | |
168 | } |