2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #ifndef LTTNG_TRIGGER_INTERNAL_H
9 #define LTTNG_TRIGGER_INTERNAL_H
11 #include <common/credentials.hpp>
12 #include <common/dynamic-array.hpp>
13 #include <common/macros.hpp>
14 #include <common/optional.hpp>
15 #include <lttng/lttng.h>
19 #include <sys/types.h>
23 struct lttng_payload_view;
25 struct mi_lttng_error_query_callbacks;
27 struct lttng_trigger {
28 /* Reference counting is only exposed to internal users. */
31 struct lttng_condition *condition;
32 struct lttng_action *action;
34 /* For now only the uid portion of the credentials is used. */
35 struct lttng_credentials creds;
38 * The unique token passed to the tracer to identify an event-rule
41 LTTNG_OPTIONAL(uint64_t) tracer_token;
44 * Is the trigger registered?
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.
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.
52 * Not considered for `is_equal`.
57 * A "hidden" trigger is a trigger that is not externally listed.
58 * It is used to hide triggers that are used internally by the session
59 * daemon so that they can't be listed nor unregistered by external
62 * This is a property that can only be set internally by the session
65 * The hidden property is preserved by copies.
67 * Note that notifications originating from an "hidden" trigger will not
68 * be sent to clients that are not within the session daemon's process.
73 * The lock is used to protect against concurrent trigger execution and
79 struct lttng_triggers {
80 struct lttng_dynamic_pointer_array array;
83 struct lttng_trigger_comm {
85 * Credentials, only the uid portion is used for now.
86 * Used as an override when desired by the root user.
90 * Length of the variable length payload (name, condition, and
94 /* Includes '\0' terminator. */
96 /* Hidden property. */
98 /* A null-terminated name, a condition, and an action follow. */
102 struct lttng_triggers_comm {
105 /* Count * lttng_trigger_comm structure */
109 ssize_t lttng_trigger_create_from_payload(struct lttng_payload_view *view,
110 struct lttng_trigger **trigger);
112 int lttng_trigger_serialize(const struct lttng_trigger *trigger,
113 struct lttng_payload *payload);
115 bool lttng_trigger_validate(const struct lttng_trigger *trigger);
117 int lttng_trigger_assign_name(
118 struct lttng_trigger *dst, const struct lttng_trigger *src);
120 void lttng_trigger_set_tracer_token(
121 struct lttng_trigger *trigger, uint64_t token);
123 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger);
125 int lttng_trigger_generate_name(struct lttng_trigger *trigger,
128 bool lttng_trigger_is_equal(
129 const struct lttng_trigger *a, const struct lttng_trigger *b);
131 bool lttng_trigger_is_hidden(const struct lttng_trigger *trigger);
133 void lttng_trigger_set_hidden(struct lttng_trigger *trigger);
135 void lttng_trigger_get(struct lttng_trigger *trigger);
137 void lttng_trigger_put(struct lttng_trigger *trigger);
140 * Serialize a trigger to a mi_writer.
141 * Return LTTNG_OK in success, other enum lttng_error_code on error.
143 enum lttng_error_code lttng_trigger_mi_serialize(const struct lttng_trigger *trigger,
144 struct mi_writer *writer,
145 const struct mi_lttng_error_query_callbacks
146 *error_query_callbacks);
149 * Allocate a new set of triggers.
150 * The returned object must be freed via lttng_triggers_destroy.
152 struct lttng_triggers *lttng_triggers_create();
155 * Return the a pointer to a mutable element at index "index" of an
156 * lttng_triggers set.
158 * This differs from the public `lttng_triggers_get_at_index` in that
159 * the returned pointer to a mutable trigger.
161 * The ownership of the trigger set element is NOT transfered.
162 * The returned object can NOT be freed via lttng_trigger_destroy.
164 struct lttng_trigger *lttng_triggers_borrow_mutable_at_index(
165 const struct lttng_triggers *triggers, unsigned int index);
168 * Add a trigger to the triggers set.
170 * A reference to the added trigger is acquired on behalf of the trigger set
173 int lttng_triggers_add(
174 struct lttng_triggers *triggers, struct lttng_trigger *trigger);
177 * Remove all triggers marked as hidden from the provided trigger set.
179 int lttng_triggers_remove_hidden_triggers(struct lttng_triggers *triggers);
182 * Serialize a trigger set to an lttng_payload object.
183 * Return LTTNG_OK on success, negative lttng error code on error.
185 int lttng_triggers_serialize(const struct lttng_triggers *triggers,
186 struct lttng_payload *payload);
188 ssize_t lttng_triggers_create_from_payload(struct lttng_payload_view *view,
189 struct lttng_triggers **triggers);
192 * Serialize a trigger set to a mi_writer.
193 * Return LTTNG_OK in success, other enum lttng_error_code on error.
195 enum lttng_error_code lttng_triggers_mi_serialize(const struct lttng_triggers *triggers,
196 struct mi_writer *writer,
197 const struct mi_lttng_error_query_callbacks
198 *error_query_callbacks);
200 const struct lttng_credentials *lttng_trigger_get_credentials(
201 const struct lttng_trigger *trigger);
203 void lttng_trigger_set_credentials(struct lttng_trigger *trigger,
204 const struct lttng_credentials *creds);
207 * Return the type of any underlying domain restriction. If no particular
208 * requirement is present, returns LTTNG_DOMAIN_NONE.
210 enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction(
211 const struct lttng_trigger *trigger);
214 * Generate any bytecode related to the trigger.
215 * On success LTTNG_OK. On error, returns lttng_error code.
217 enum lttng_error_code lttng_trigger_generate_bytecode(
218 struct lttng_trigger *trigger,
219 const struct lttng_credentials *creds);
222 * Note that the trigger object is not locked by "copy" as it is const and
223 * used with a number of 'const' triggers. If the trigger could be shared at
224 * the moment of the copy, it is the caller's responsability to lock it for
225 * the duration of the copy.
227 struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger);
230 * A given trigger needs a tracer notifier if
231 * it has an event-rule condition,
233 * it has one or more sessiond-execution action.
235 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger);
237 void lttng_trigger_set_as_registered(struct lttng_trigger *trigger);
239 void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger);
242 * The trigger must be locked before calling lttng_trigger_is_registered.
244 * The lock is necessary since a trigger can be unregistered at any time.
246 * Manipulations requiring that the trigger be registered must always acquire
247 * the trigger lock for the duration of the manipulation using
248 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
250 bool lttng_trigger_is_registered(struct lttng_trigger *trigger);
252 void lttng_trigger_lock(struct lttng_trigger *trigger);
254 void lttng_trigger_unlock(struct lttng_trigger *trigger);
256 enum lttng_trigger_status lttng_trigger_add_error_results(
257 const struct lttng_trigger *trigger,
258 struct lttng_error_query_results *results);
260 enum lttng_trigger_status lttng_trigger_condition_add_error_results(
261 const struct lttng_trigger *trigger,
262 struct lttng_error_query_results *results);
264 enum lttng_trigger_status lttng_trigger_add_action_error_query_results(
265 struct lttng_trigger *trigger,
266 struct lttng_error_query_results *results);
269 * Set the trigger name.
271 * A name is optional.
272 * A name will be assigned on trigger registration if no name is set.
274 * The name is copied.
276 * Return LTTNG_TRIGGER_STATUS_OK on success, LTTNG_TRIGGER_STATUS_INVALID
277 * if invalid parameters are passed.
279 enum lttng_trigger_status lttng_trigger_set_name(
280 struct lttng_trigger *trigger, const char *name);
282 #endif /* LTTNG_TRIGGER_INTERNAL_H */