5ae84f3d9e8f6ef28cb6323cd8b83c62101291f8
[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 if (!trigger->creds.uid.is_set) {
29 valid = false;
30 goto end;
31 }
32
33 valid = lttng_condition_validate(trigger->condition) &&
34 lttng_action_validate(trigger->action);
35 end:
36 return valid;
37 }
38
39 struct lttng_trigger *lttng_trigger_create(
40 struct lttng_condition *condition,
41 struct lttng_action *action)
42 {
43 struct lttng_trigger *trigger = NULL;
44
45 if (!condition || !action) {
46 goto end;
47 }
48
49 trigger = zmalloc(sizeof(struct lttng_trigger));
50 if (!trigger) {
51 goto end;
52 }
53
54 urcu_ref_init(&trigger->ref);
55
56 lttng_condition_get(condition);
57 trigger->condition = condition;
58
59 lttng_action_get(action);
60 trigger->action = action;
61
62 end:
63 return trigger;
64 }
65
66 /*
67 * Note: the lack of reference counting 'get' on the condition object is normal.
68 * This API was exposed as such in 2.11. The client is not expected to call
69 * lttng_condition_destroy on the returned object.
70 */
71 struct lttng_condition *lttng_trigger_get_condition(
72 struct lttng_trigger *trigger)
73 {
74 return trigger ? trigger->condition : NULL;
75 }
76
77 LTTNG_HIDDEN
78 const struct lttng_condition *lttng_trigger_get_const_condition(
79 const struct lttng_trigger *trigger)
80 {
81 return trigger->condition;
82 }
83
84
85 /*
86 * Note: the lack of reference counting 'get' on the action object is normal.
87 * This API was exposed as such in 2.11. The client is not expected to call
88 * lttng_action_destroy on the returned object.
89 */
90 struct lttng_action *lttng_trigger_get_action(
91 struct lttng_trigger *trigger)
92 {
93 return trigger ? trigger->action : NULL;
94 }
95
96 LTTNG_HIDDEN
97 const struct lttng_action *lttng_trigger_get_const_action(
98 const struct lttng_trigger *trigger)
99 {
100 return trigger->action;
101 }
102
103 static void trigger_destroy_ref(struct urcu_ref *ref)
104 {
105 struct lttng_trigger *trigger =
106 container_of(ref, struct lttng_trigger, ref);
107 struct lttng_action *action = lttng_trigger_get_action(trigger);
108 struct lttng_condition *condition =
109 lttng_trigger_get_condition(trigger);
110
111 assert(action);
112 assert(condition);
113
114 /* Release ownership. */
115 lttng_action_put(action);
116 lttng_condition_put(condition);
117
118 free(trigger);
119 }
120
121 void lttng_trigger_destroy(struct lttng_trigger *trigger)
122 {
123 lttng_trigger_put(trigger);
124 }
125
126 LTTNG_HIDDEN
127 ssize_t lttng_trigger_create_from_payload(
128 struct lttng_payload_view *src_view,
129 struct lttng_trigger **trigger)
130 {
131 ssize_t ret, offset = 0, condition_size, action_size;
132 struct lttng_condition *condition = NULL;
133 struct lttng_action *action = NULL;
134 const struct lttng_trigger_comm *trigger_comm;
135 struct lttng_credentials creds = {
136 .uid = LTTNG_OPTIONAL_INIT_UNSET,
137 .gid = LTTNG_OPTIONAL_INIT_UNSET,
138 };
139
140 if (!src_view || !trigger) {
141 ret = -1;
142 goto end;
143 }
144
145 /* lttng_trigger_comm header */
146 trigger_comm = (typeof(trigger_comm)) src_view->buffer.data;
147
148 /* Set the trigger's creds. */
149 if (trigger_comm->uid > (uint64_t) ((uid_t) -1)) {
150 /* UID out of range for this platform. */
151 ret = -1;
152 goto end;
153 }
154
155 LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid);
156
157 offset += sizeof(*trigger_comm);
158 {
159 /* struct lttng_condition */
160 struct lttng_payload_view condition_view =
161 lttng_payload_view_from_view(
162 src_view, offset, -1);
163
164 condition_size = lttng_condition_create_from_payload(&condition_view,
165 &condition);
166 }
167
168 if (condition_size < 0) {
169 ret = condition_size;
170 goto end;
171 }
172
173 offset += condition_size;
174 {
175 /* struct lttng_action */
176 struct lttng_payload_view action_view =
177 lttng_payload_view_from_view(
178 src_view, offset, -1);
179
180 action_size = lttng_action_create_from_payload(&action_view, &action);
181 }
182
183 if (action_size < 0) {
184 ret = action_size;
185 goto end;
186 }
187 offset += action_size;
188
189 /* Unexpected size of inner-elements; the buffer is corrupted. */
190 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
191 ret = -1;
192 goto error;
193 }
194
195 *trigger = lttng_trigger_create(condition, action);
196 if (!*trigger) {
197 ret = -1;
198 goto error;
199 }
200
201 lttng_trigger_set_credentials(*trigger, &creds);
202
203 /*
204 * The trigger object owns references to the action and condition
205 * objects.
206 */
207 lttng_condition_put(condition);
208 condition = NULL;
209
210 lttng_action_put(action);
211 action = NULL;
212
213 ret = offset;
214
215 error:
216 lttng_condition_destroy(condition);
217 lttng_action_destroy(action);
218 end:
219 return ret;
220 }
221
222 /*
223 * Both elements are stored contiguously, see their "*_comm" structure
224 * for the detailed format.
225 */
226 LTTNG_HIDDEN
227 int lttng_trigger_serialize(struct lttng_trigger *trigger,
228 struct lttng_payload *payload)
229 {
230 int ret;
231 size_t header_offset, size_before_payload;
232 struct lttng_trigger_comm trigger_comm = {};
233 struct lttng_trigger_comm *header;
234 const struct lttng_credentials *creds = NULL;
235
236 creds = lttng_trigger_get_credentials(trigger);
237 assert(creds);
238
239 trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
240
241 header_offset = payload->buffer.size;
242 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
243 sizeof(trigger_comm));
244 if (ret) {
245 goto end;
246 }
247
248 size_before_payload = payload->buffer.size;
249 ret = lttng_condition_serialize(trigger->condition, payload);
250 if (ret) {
251 goto end;
252 }
253
254 ret = lttng_action_serialize(trigger->action, payload);
255 if (ret) {
256 goto end;
257 }
258
259 /* Update payload size. */
260 header = (typeof(header)) (payload->buffer.data + header_offset);
261 header->length = payload->buffer.size - size_before_payload;
262 end:
263 return ret;
264 }
265
266 LTTNG_HIDDEN
267 void lttng_trigger_get(struct lttng_trigger *trigger)
268 {
269 urcu_ref_get(&trigger->ref);
270 }
271
272 LTTNG_HIDDEN
273 void lttng_trigger_put(struct lttng_trigger *trigger)
274 {
275 if (!trigger) {
276 return;
277 }
278
279 urcu_ref_put(&trigger->ref , trigger_destroy_ref);
280 }
281
282 LTTNG_HIDDEN
283 const struct lttng_credentials *lttng_trigger_get_credentials(
284 const struct lttng_trigger *trigger)
285 {
286 return &trigger->creds;
287 }
288
289 LTTNG_HIDDEN
290 void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
291 const struct lttng_credentials *creds)
292 {
293 assert(creds);
294 trigger->creds = *creds;
295 }
296
297 enum lttng_trigger_status lttng_trigger_set_owner_uid(
298 struct lttng_trigger *trigger, uid_t uid)
299 {
300 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
301 const struct lttng_credentials creds = {
302 .uid = LTTNG_OPTIONAL_INIT_VALUE(uid),
303 .gid = LTTNG_OPTIONAL_INIT_UNSET,
304 };
305
306 if (!trigger) {
307 ret = LTTNG_TRIGGER_STATUS_INVALID;
308 goto end;
309 }
310
311 /* Client-side validation only to report a clearer error. */
312 if (geteuid() != 0) {
313 ret = LTTNG_TRIGGER_STATUS_PERMISSION_DENIED;
314 goto end;
315 }
316
317 lttng_trigger_set_credentials(trigger, &creds);
318
319 end:
320 return ret;
321 }
322
323 enum lttng_trigger_status lttng_trigger_get_owner_uid(
324 const struct lttng_trigger *trigger, uid_t *uid)
325 {
326 enum lttng_trigger_status ret = LTTNG_TRIGGER_STATUS_OK;
327 const struct lttng_credentials *creds = NULL;
328
329 if (!trigger || !uid ) {
330 ret = LTTNG_TRIGGER_STATUS_INVALID;
331 goto end;
332 }
333
334 if (!trigger->creds.uid.is_set ) {
335 ret = LTTNG_TRIGGER_STATUS_UNSET;
336 goto end;
337 }
338
339 creds = lttng_trigger_get_credentials(trigger);
340 *uid = lttng_credentials_get_uid(creds);
341
342 end:
343 return ret;
344 }
This page took 0.05297 seconds and 3 git commands to generate.