MI: implement all objects related to trigger machine interface
[lttng-tools.git] / include / lttng / trigger / trigger-internal.h
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#ifndef LTTNG_TRIGGER_INTERNAL_H
9#define LTTNG_TRIGGER_INTERNAL_H
10
3da864a9 11#include <common/credentials.h>
a02903c0 12#include <common/dynamic-array.h>
a58c490f 13#include <common/macros.h>
3da864a9 14#include <common/optional.h>
588c4b0d 15#include <lttng/lttng.h>
9c374932 16#include <pthread.h>
a58c490f 17#include <stdbool.h>
9c374932 18#include <stdint.h>
72756a3f 19#include <sys/types.h>
f01d28b4 20#include <urcu/ref.h>
a58c490f 21
c0a66c84
JG
22struct lttng_payload;
23struct lttng_payload_view;
6a751b95
JR
24struct mi_writer;
25struct mi_lttng_error_query_callbacks;
c0a66c84 26
a58c490f 27struct lttng_trigger {
f01d28b4
JR
28 /* Reference counting is only exposed to internal users. */
29 struct urcu_ref ref;
30
a58c490f
JG
31 struct lttng_condition *condition;
32 struct lttng_action *action;
242388e4 33 char *name;
64eafdf6
JR
34 /* For now only the uid portion of the credentials is used. */
35 struct lttng_credentials creds;
e6887944
JR
36 /*
37 * Internal use only.
38 * The unique token passed to the tracer to identify an event-rule
39 * notification.
40 */
41 LTTNG_OPTIONAL(uint64_t) tracer_token;
9c374932
JR
42
43 /*
44 * Is the trigger registered?
45 *
46 * This is necessary since a reference holder might be interested in the
47 * overall state of the trigger from the point of view of its owner.
48 *
49 * The main user is the action executor since we want to prevent the
50 * execution of actions related to a trigger that is unregistered.
51 *
52 * Not considered for `is_equal`.
53 */
54 bool registered;
55
56 /*
57 * The lock is used to protect against concurrent trigger execution and
58 * trigger removal.
59 */
60 pthread_mutex_t lock;
a58c490f
JG
61};
62
a02903c0
JR
63struct lttng_triggers {
64 struct lttng_dynamic_pointer_array array;
65};
66
a58c490f 67struct lttng_trigger_comm {
64eafdf6
JR
68 /*
69 * Credentials, only the uid portion is used for now.
70 * Used as an override when desired by the root user.
71 */
72 uint64_t uid;
242388e4
JR
73 /*
74 * Length of the variable length payload (name, condition, and
75 * an action).
76 */
77 uint32_t length;
78 /* Includes '\0' terminator. */
79 uint32_t name_length;
80 /* A null-terminated name, a condition, and an action follow. */
a58c490f
JG
81 char payload[];
82} LTTNG_PACKED;
83
a02903c0
JR
84struct lttng_triggers_comm {
85 uint32_t count;
86 uint32_t length;
87 /* Count * lttng_trigger_comm structure */
88 char payload[];
89};
90
a58c490f 91LTTNG_HIDDEN
c0a66c84 92ssize_t lttng_trigger_create_from_payload(struct lttng_payload_view *view,
a58c490f
JG
93 struct lttng_trigger **trigger);
94
95LTTNG_HIDDEN
a02903c0 96int lttng_trigger_serialize(const struct lttng_trigger *trigger,
c0a66c84 97 struct lttng_payload *payload);
a58c490f
JG
98
99LTTNG_HIDDEN
b61776fb 100bool lttng_trigger_validate(const struct lttng_trigger *trigger);
a58c490f 101
242388e4
JR
102LTTNG_HIDDEN
103int lttng_trigger_assign_name(
104 struct lttng_trigger *dst, const struct lttng_trigger *src);
105
e6887944
JR
106LTTNG_HIDDEN
107void lttng_trigger_set_tracer_token(
108 struct lttng_trigger *trigger, uint64_t token);
109
110LTTNG_HIDDEN
111uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger);
112
242388e4
JR
113LTTNG_HIDDEN
114int lttng_trigger_generate_name(struct lttng_trigger *trigger,
115 uint64_t unique_id);
116
85c06c44
JR
117LTTNG_HIDDEN
118bool lttng_trigger_is_equal(
119 const struct lttng_trigger *a, const struct lttng_trigger *b);
120
f01d28b4
JR
121LTTNG_HIDDEN
122void lttng_trigger_get(struct lttng_trigger *trigger);
123
124LTTNG_HIDDEN
125void lttng_trigger_put(struct lttng_trigger *trigger);
126
6a751b95
JR
127/*
128 * Serialize a trigger to a mi_writer.
129 * Return LTTNG_OK in success, other enum lttng_error_code on error.
130 */
131LTTNG_HIDDEN
132enum lttng_error_code lttng_trigger_mi_serialize(const struct lttng_trigger *trigger,
133 struct mi_writer *writer,
134 const struct mi_lttng_error_query_callbacks
135 *error_query_callbacks);
136
a02903c0
JR
137/*
138 * Allocate a new set of triggers.
139 * The returned object must be freed via lttng_triggers_destroy.
140 */
141LTTNG_HIDDEN
142struct lttng_triggers *lttng_triggers_create(void);
143
144/*
145 * Return the a pointer to a mutable element at index "index" of an
146 * lttng_triggers set.
147 *
148 * This differs from the public `lttng_triggers_get_at_index` in that
149 * the returned pointer to a mutable trigger.
150 *
151 * The ownership of the trigger set element is NOT transfered.
152 * The returned object can NOT be freed via lttng_trigger_destroy.
153 */
154LTTNG_HIDDEN
155struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
156 const struct lttng_triggers *triggers, unsigned int index);
157
158/*
159 * Add a trigger to the triggers set.
160 *
161 * A reference to the added trigger is acquired on behalf of the trigger set
162 * on success.
163 */
164LTTNG_HIDDEN
165int lttng_triggers_add(
166 struct lttng_triggers *triggers, struct lttng_trigger *trigger);
167
168/*
169 * Serialize a trigger set to an lttng_payload object.
170 * Return LTTNG_OK on success, negative lttng error code on error.
171 */
172LTTNG_HIDDEN
173int lttng_triggers_serialize(const struct lttng_triggers *triggers,
174 struct lttng_payload *payload);
175
176LTTNG_HIDDEN
177ssize_t lttng_triggers_create_from_payload(struct lttng_payload_view *view,
178 struct lttng_triggers **triggers);
179
6a751b95
JR
180/*
181 * Serialize a trigger set to a mi_writer.
182 * Return LTTNG_OK in success, other enum lttng_error_code on error.
183 */
184LTTNG_HIDDEN
185enum lttng_error_code lttng_triggers_mi_serialize(const struct lttng_triggers *triggers,
186 struct mi_writer *writer,
187 const struct mi_lttng_error_query_callbacks
188 *error_query_callbacks);
189
3da864a9
JR
190LTTNG_HIDDEN
191const struct lttng_credentials *lttng_trigger_get_credentials(
192 const struct lttng_trigger *trigger);
193
194LTTNG_HIDDEN
64eafdf6 195void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
3da864a9
JR
196 const struct lttng_credentials *creds);
197
91c96f62
JR
198/*
199 * Return the type of any underlying domain restriction. If no particular
200 * requirement is present, returns LTTNG_DOMAIN_NONE.
201 */
202LTTNG_HIDDEN
203enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
204 const struct lttng_trigger *trigger);
205
58daac01
JR
206/*
207 * Generate any bytecode related to the trigger.
208 * On success LTTNG_OK. On error, returns lttng_error code.
209 */
210LTTNG_HIDDEN
211enum lttng_error_code lttng_trigger_generate_bytecode(
212 struct lttng_trigger *trigger,
213 const struct lttng_credentials *creds);
214
94dbd8e4
JG
215/*
216 * Note that the trigger object is not locked by "copy" as it is const and
217 * used with a number of 'const' triggers. If the trigger could be shared at
218 * the moment of the copy, it is the caller's responsability to lock it for
219 * the duration of the copy.
220 */
b61776fb
SM
221LTTNG_HIDDEN
222struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger);
223
c738df17
FD
224/*
225 * A given trigger needs a tracer notifier if
226 * it has an event-rule condition,
227 * AND
228 * it has one or more sessiond-execution action.
229 */
230LTTNG_HIDDEN
231bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger);
232
9c374932
JR
233LTTNG_HIDDEN
234void lttng_trigger_set_as_registered(struct lttng_trigger *trigger);
235
236LTTNG_HIDDEN
237void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger);
238
239/*
240 * The trigger must be locked before calling lttng_trigger_is_registered.
241 *
242 * The lock is necessary since a trigger can be unregistered at any time.
243 *
244 * Manipulations requiring that the trigger be registered must always acquire
245 * the trigger lock for the duration of the manipulation using
246 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
247 */
248LTTNG_HIDDEN
249bool lttng_trigger_is_registered(struct lttng_trigger *trigger);
250
251LTTNG_HIDDEN
252void lttng_trigger_lock(struct lttng_trigger *trigger);
253
254LTTNG_HIDDEN
255void lttng_trigger_unlock(struct lttng_trigger *trigger);
256
588c4b0d
JG
257LTTNG_HIDDEN
258enum lttng_trigger_status lttng_trigger_add_error_results(
259 const struct lttng_trigger *trigger,
260 struct lttng_error_query_results *results);
261
63dd3d7b
JG
262LTTNG_HIDDEN
263enum lttng_trigger_status lttng_trigger_condition_add_error_results(
264 const struct lttng_trigger *trigger,
265 struct lttng_error_query_results *results);
266
588c4b0d
JG
267LTTNG_HIDDEN
268enum lttng_trigger_status lttng_trigger_add_action_error_query_results(
269 struct lttng_trigger *trigger,
270 struct lttng_error_query_results *results);
271
a5c2d2a7
JG
272/*
273 * Set the trigger name.
274 *
275 * A name is optional.
276 * A name will be assigned on trigger registration if no name is set.
277 *
278 * The name is copied.
279 *
280 * Return LTTNG_TRIGGER_STATUS_OK on success, LTTNG_TRIGGER_STATUS_INVALID
281 * if invalid parameters are passed.
282 */
283LTTNG_HIDDEN
284enum lttng_trigger_status lttng_trigger_set_name(
285 struct lttng_trigger *trigger, const char *name);
286
a58c490f 287#endif /* LTTNG_TRIGGER_INTERNAL_H */
This page took 0.042707 seconds and 4 git commands to generate.