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 | #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 |
22 | struct lttng_payload; |
23 | struct lttng_payload_view; | |
6a751b95 JR |
24 | struct mi_writer; |
25 | struct mi_lttng_error_query_callbacks; | |
c0a66c84 | 26 | |
a58c490f | 27 | struct 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 |
63 | struct lttng_triggers { |
64 | struct lttng_dynamic_pointer_array array; | |
65 | }; | |
66 | ||
a58c490f | 67 | struct 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 |
84 | struct lttng_triggers_comm { |
85 | uint32_t count; | |
86 | uint32_t length; | |
87 | /* Count * lttng_trigger_comm structure */ | |
88 | char payload[]; | |
89 | }; | |
90 | ||
a58c490f | 91 | LTTNG_HIDDEN |
c0a66c84 | 92 | ssize_t lttng_trigger_create_from_payload(struct lttng_payload_view *view, |
a58c490f JG |
93 | struct lttng_trigger **trigger); |
94 | ||
95 | LTTNG_HIDDEN | |
a02903c0 | 96 | int lttng_trigger_serialize(const struct lttng_trigger *trigger, |
c0a66c84 | 97 | struct lttng_payload *payload); |
a58c490f JG |
98 | |
99 | LTTNG_HIDDEN | |
b61776fb | 100 | bool lttng_trigger_validate(const struct lttng_trigger *trigger); |
a58c490f | 101 | |
242388e4 JR |
102 | LTTNG_HIDDEN |
103 | int lttng_trigger_assign_name( | |
104 | struct lttng_trigger *dst, const struct lttng_trigger *src); | |
105 | ||
e6887944 JR |
106 | LTTNG_HIDDEN |
107 | void lttng_trigger_set_tracer_token( | |
108 | struct lttng_trigger *trigger, uint64_t token); | |
109 | ||
110 | LTTNG_HIDDEN | |
111 | uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger *trigger); | |
112 | ||
242388e4 JR |
113 | LTTNG_HIDDEN |
114 | int lttng_trigger_generate_name(struct lttng_trigger *trigger, | |
115 | uint64_t unique_id); | |
116 | ||
85c06c44 JR |
117 | LTTNG_HIDDEN |
118 | bool lttng_trigger_is_equal( | |
119 | const struct lttng_trigger *a, const struct lttng_trigger *b); | |
120 | ||
f01d28b4 JR |
121 | LTTNG_HIDDEN |
122 | void lttng_trigger_get(struct lttng_trigger *trigger); | |
123 | ||
124 | LTTNG_HIDDEN | |
125 | void 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 | */ | |
131 | LTTNG_HIDDEN | |
132 | enum 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 | */ | |
141 | LTTNG_HIDDEN | |
142 | struct 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 | */ | |
154 | LTTNG_HIDDEN | |
155 | struct 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 | */ | |
164 | LTTNG_HIDDEN | |
165 | int 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 | */ | |
172 | LTTNG_HIDDEN | |
173 | int lttng_triggers_serialize(const struct lttng_triggers *triggers, | |
174 | struct lttng_payload *payload); | |
175 | ||
176 | LTTNG_HIDDEN | |
177 | ssize_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 | */ | |
184 | LTTNG_HIDDEN | |
185 | enum 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 |
190 | LTTNG_HIDDEN |
191 | const struct lttng_credentials *lttng_trigger_get_credentials( | |
192 | const struct lttng_trigger *trigger); | |
193 | ||
194 | LTTNG_HIDDEN | |
64eafdf6 | 195 | void 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 | */ | |
202 | LTTNG_HIDDEN | |
203 | enum 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 | */ | |
210 | LTTNG_HIDDEN | |
211 | enum 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 |
221 | LTTNG_HIDDEN |
222 | struct 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 | */ | |
230 | LTTNG_HIDDEN | |
231 | bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger); | |
232 | ||
9c374932 JR |
233 | LTTNG_HIDDEN |
234 | void lttng_trigger_set_as_registered(struct lttng_trigger *trigger); | |
235 | ||
236 | LTTNG_HIDDEN | |
237 | void 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 | */ | |
248 | LTTNG_HIDDEN | |
249 | bool lttng_trigger_is_registered(struct lttng_trigger *trigger); | |
250 | ||
251 | LTTNG_HIDDEN | |
252 | void lttng_trigger_lock(struct lttng_trigger *trigger); | |
253 | ||
254 | LTTNG_HIDDEN | |
255 | void lttng_trigger_unlock(struct lttng_trigger *trigger); | |
256 | ||
588c4b0d JG |
257 | LTTNG_HIDDEN |
258 | enum lttng_trigger_status lttng_trigger_add_error_results( | |
259 | const struct lttng_trigger *trigger, | |
260 | struct lttng_error_query_results *results); | |
261 | ||
63dd3d7b JG |
262 | LTTNG_HIDDEN |
263 | enum 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 |
267 | LTTNG_HIDDEN |
268 | enum 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 | */ | |
283 | LTTNG_HIDDEN | |
284 | enum lttng_trigger_status lttng_trigger_set_name( | |
285 | struct lttng_trigger *trigger, const char *name); | |
286 | ||
a58c490f | 287 | #endif /* LTTNG_TRIGGER_INTERNAL_H */ |