trigger: use condition and action ref counting to ease internal objects management
[lttng-tools.git] / src / common / trigger.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/trigger/trigger-internal.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/action/action-internal.h>
11 #include <common/credentials.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <common/error.h>
15 #include <common/optional.h>
16 #include <assert.h>
17
18 LTTNG_HIDDEN
19 bool lttng_trigger_validate(struct lttng_trigger *trigger)
20 {
21 bool valid;
22
23 if (!trigger) {
24 valid = false;
25 goto end;
26 }
27
28 valid = lttng_condition_validate(trigger->condition) &&
29 lttng_action_validate(trigger->action);
30 end:
31 return valid;
32 }
33
34 struct lttng_trigger *lttng_trigger_create(
35 struct lttng_condition *condition,
36 struct lttng_action *action)
37 {
38 struct lttng_trigger *trigger = NULL;
39
40 if (!condition || !action) {
41 goto end;
42 }
43
44 trigger = zmalloc(sizeof(struct lttng_trigger));
45 if (!trigger) {
46 goto end;
47 }
48
49 lttng_condition_get(condition);
50 trigger->condition = condition;
51
52 lttng_action_get(action);
53 trigger->action = action;
54
55 end:
56 return trigger;
57 }
58
59 /*
60 * Note: the lack of reference counting 'get' on the condition object is normal.
61 * This API was exposed as such in 2.11. The client is not expected to call
62 * lttng_condition_destroy on the returned object.
63 */
64 struct lttng_condition *lttng_trigger_get_condition(
65 struct lttng_trigger *trigger)
66 {
67 return trigger ? trigger->condition : NULL;
68 }
69
70 LTTNG_HIDDEN
71 const struct lttng_condition *lttng_trigger_get_const_condition(
72 const struct lttng_trigger *trigger)
73 {
74 return trigger->condition;
75 }
76
77
78 /*
79 * Note: the lack of reference counting 'get' on the action object is normal.
80 * This API was exposed as such in 2.11. The client is not expected to call
81 * lttng_action_destroy on the returned object.
82 */
83 struct lttng_action *lttng_trigger_get_action(
84 struct lttng_trigger *trigger)
85 {
86 return trigger ? trigger->action : NULL;
87 }
88
89 LTTNG_HIDDEN
90 const struct lttng_action *lttng_trigger_get_const_action(
91 const struct lttng_trigger *trigger)
92 {
93 return trigger->action;
94 }
95
96 void lttng_trigger_destroy(struct lttng_trigger *trigger)
97 {
98 struct lttng_action *action = lttng_trigger_get_action(trigger);
99 struct lttng_condition *condition =
100 lttng_trigger_get_condition(trigger);
101
102 if (!trigger) {
103 return;
104 }
105
106 assert(action);
107 assert(condition);
108
109 /* Release ownership. */
110 lttng_action_put(action);
111 lttng_condition_put(condition);
112
113 free(trigger);
114 }
115
116 LTTNG_HIDDEN
117 ssize_t lttng_trigger_create_from_payload(
118 struct lttng_payload_view *src_view,
119 struct lttng_trigger **trigger)
120 {
121 ssize_t ret, offset = 0, condition_size, action_size;
122 struct lttng_condition *condition = NULL;
123 struct lttng_action *action = NULL;
124 const struct lttng_trigger_comm *trigger_comm;
125
126 if (!src_view || !trigger) {
127 ret = -1;
128 goto end;
129 }
130
131 /* lttng_trigger_comm header */
132 trigger_comm = (typeof(trigger_comm)) src_view->buffer.data;
133 offset += sizeof(*trigger_comm);
134 {
135 /* struct lttng_condition */
136 struct lttng_payload_view condition_view =
137 lttng_payload_view_from_view(
138 src_view, offset, -1);
139
140 condition_size = lttng_condition_create_from_payload(&condition_view,
141 &condition);
142 }
143
144 if (condition_size < 0) {
145 ret = condition_size;
146 goto end;
147 }
148
149 offset += condition_size;
150 {
151 /* struct lttng_action */
152 struct lttng_payload_view action_view =
153 lttng_payload_view_from_view(
154 src_view, offset, -1);
155
156 action_size = lttng_action_create_from_payload(&action_view, &action);
157 }
158
159 if (action_size < 0) {
160 ret = action_size;
161 goto end;
162 }
163 offset += action_size;
164
165 /* Unexpected size of inner-elements; the buffer is corrupted. */
166 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
167 ret = -1;
168 goto error;
169 }
170
171 *trigger = lttng_trigger_create(condition, action);
172 if (!*trigger) {
173 ret = -1;
174 goto error;
175 }
176
177 /*
178 * The trigger object owns references to the action and condition
179 * objects.
180 */
181 lttng_condition_put(condition);
182 condition = NULL;
183
184 lttng_action_put(action);
185 action = NULL;
186
187 ret = offset;
188
189 error:
190 lttng_condition_destroy(condition);
191 lttng_action_destroy(action);
192 end:
193 return ret;
194 }
195
196 /*
197 * Both elements are stored contiguously, see their "*_comm" structure
198 * for the detailed format.
199 */
200 LTTNG_HIDDEN
201 int lttng_trigger_serialize(struct lttng_trigger *trigger,
202 struct lttng_payload *payload)
203 {
204 int ret;
205 size_t header_offset, size_before_payload;
206 struct lttng_trigger_comm trigger_comm = {};
207 struct lttng_trigger_comm *header;
208
209 header_offset = payload->buffer.size;
210 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
211 sizeof(trigger_comm));
212 if (ret) {
213 goto end;
214 }
215
216 size_before_payload = payload->buffer.size;
217 ret = lttng_condition_serialize(trigger->condition, payload);
218 if (ret) {
219 goto end;
220 }
221
222 ret = lttng_action_serialize(trigger->action, payload);
223 if (ret) {
224 goto end;
225 }
226
227 /* Update payload size. */
228 header = (typeof(header)) (payload->buffer.data + header_offset);
229 header->length = payload->buffer.size - size_before_payload;
230 end:
231 return ret;
232 }
233
234 LTTNG_HIDDEN
235 const struct lttng_credentials *lttng_trigger_get_credentials(
236 const struct lttng_trigger *trigger)
237 {
238 return LTTNG_OPTIONAL_GET_PTR(trigger->creds);
239 }
240
241 LTTNG_HIDDEN
242 void lttng_trigger_set_credentials(
243 struct lttng_trigger *trigger,
244 const struct lttng_credentials *creds)
245 {
246 assert(creds);
247 LTTNG_OPTIONAL_SET(&trigger->creds, *creds);
248 }
This page took 0.038735 seconds and 6 git commands to generate.