Commit | Line | Data |
---|---|---|
ab0ee2ca | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
ab0ee2ca | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
ab0ee2ca | 5 | * |
ab0ee2ca JG |
6 | */ |
7 | ||
f2b3ef9f JG |
8 | #include "lttng/action/action.h" |
9 | #include "lttng/trigger/trigger-internal.h" | |
ab0ee2ca JG |
10 | #define _LGPL_SOURCE |
11 | #include <urcu.h> | |
12 | #include <urcu/rculfhash.h> | |
13 | ||
ab0ee2ca JG |
14 | #include <common/defaults.h> |
15 | #include <common/error.h> | |
16 | #include <common/futex.h> | |
17 | #include <common/unix.h> | |
18 | #include <common/dynamic-buffer.h> | |
19 | #include <common/hashtable/utils.h> | |
20 | #include <common/sessiond-comm/sessiond-comm.h> | |
21 | #include <common/macros.h> | |
22 | #include <lttng/condition/condition.h> | |
9b63a4aa | 23 | #include <lttng/action/action-internal.h> |
ad63a966 | 24 | #include <lttng/action/list-internal.h> |
93ac2c44 | 25 | #include <lttng/domain-internal.h> |
ab0ee2ca JG |
26 | #include <lttng/notification/notification-internal.h> |
27 | #include <lttng/condition/condition-internal.h> | |
28 | #include <lttng/condition/buffer-usage-internal.h> | |
e8360425 | 29 | #include <lttng/condition/session-consumed-size-internal.h> |
ea9a44f0 | 30 | #include <lttng/condition/session-rotation-internal.h> |
670a26e4 | 31 | #include <lttng/condition/event-rule-matches-internal.h> |
d02d7404 | 32 | #include <lttng/domain-internal.h> |
ab0ee2ca | 33 | #include <lttng/notification/channel-internal.h> |
85c06c44 | 34 | #include <lttng/trigger/trigger-internal.h> |
959e3c66 | 35 | #include <lttng/event-rule/event-rule-internal.h> |
1da26331 | 36 | |
ab0ee2ca JG |
37 | #include <time.h> |
38 | #include <unistd.h> | |
39 | #include <assert.h> | |
40 | #include <inttypes.h> | |
d53ea4e4 | 41 | #include <fcntl.h> |
ab0ee2ca | 42 | |
e98a976a | 43 | #include "condition-internal.h" |
90aa04a1 | 44 | #include "event-notifier-error-accounting.h" |
1da26331 JG |
45 | #include "notification-thread.h" |
46 | #include "notification-thread-events.h" | |
47 | #include "notification-thread-commands.h" | |
48 | #include "lttng-sessiond.h" | |
49 | #include "kernel.h" | |
50 | ||
ab0ee2ca JG |
51 | #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
52 | #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT) | |
53 | ||
82b3cbf4 JR |
54 | /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */ |
55 | #define MAX_CAPTURE_SIZE (PIPE_BUF) | |
56 | ||
51eab943 JG |
57 | enum lttng_object_type { |
58 | LTTNG_OBJECT_TYPE_UNKNOWN, | |
59 | LTTNG_OBJECT_TYPE_NONE, | |
60 | LTTNG_OBJECT_TYPE_CHANNEL, | |
61 | LTTNG_OBJECT_TYPE_SESSION, | |
62 | }; | |
63 | ||
ab0ee2ca | 64 | struct lttng_trigger_list_element { |
9e0bb80e | 65 | /* No ownership of the trigger object is assumed. */ |
f2b3ef9f | 66 | struct lttng_trigger *trigger; |
ab0ee2ca JG |
67 | struct cds_list_head node; |
68 | }; | |
69 | ||
70 | struct lttng_channel_trigger_list { | |
71 | struct channel_key channel_key; | |
ea9a44f0 | 72 | /* List of struct lttng_trigger_list_element. */ |
ab0ee2ca | 73 | struct cds_list_head list; |
ea9a44f0 | 74 | /* Node in the channel_triggers_ht */ |
ab0ee2ca | 75 | struct cds_lfht_node channel_triggers_ht_node; |
83b934ad MD |
76 | /* call_rcu delayed reclaim. */ |
77 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
78 | }; |
79 | ||
ea9a44f0 JG |
80 | /* |
81 | * List of triggers applying to a given session. | |
82 | * | |
83 | * See: | |
84 | * - lttng_session_trigger_list_create() | |
85 | * - lttng_session_trigger_list_build() | |
86 | * - lttng_session_trigger_list_destroy() | |
87 | * - lttng_session_trigger_list_add() | |
88 | */ | |
89 | struct lttng_session_trigger_list { | |
90 | /* | |
91 | * Not owned by this; points to the session_info structure's | |
92 | * session name. | |
93 | */ | |
94 | const char *session_name; | |
95 | /* List of struct lttng_trigger_list_element. */ | |
96 | struct cds_list_head list; | |
97 | /* Node in the session_triggers_ht */ | |
98 | struct cds_lfht_node session_triggers_ht_node; | |
99 | /* | |
100 | * Weak reference to the notification system's session triggers | |
101 | * hashtable. | |
102 | * | |
103 | * The session trigger list structure structure is owned by | |
104 | * the session's session_info. | |
105 | * | |
106 | * The session_info is kept alive the the channel_infos holding a | |
107 | * reference to it (reference counting). When those channels are | |
108 | * destroyed (at runtime or on teardown), the reference they hold | |
109 | * to the session_info are released. On destruction of session_info, | |
110 | * session_info_destroy() will remove the list of triggers applying | |
111 | * to this session from the notification system's state. | |
112 | * | |
113 | * This implies that the session_triggers_ht must be destroyed | |
114 | * after the channels. | |
115 | */ | |
116 | struct cds_lfht *session_triggers_ht; | |
117 | /* Used for delayed RCU reclaim. */ | |
118 | struct rcu_head rcu_node; | |
119 | }; | |
120 | ||
ab0ee2ca JG |
121 | struct lttng_trigger_ht_element { |
122 | struct lttng_trigger *trigger; | |
123 | struct cds_lfht_node node; | |
242388e4 | 124 | struct cds_lfht_node node_by_name_uid; |
091fa780 | 125 | struct cds_list_head client_list_trigger_node; |
83b934ad MD |
126 | /* call_rcu delayed reclaim. */ |
127 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
128 | }; |
129 | ||
130 | struct lttng_condition_list_element { | |
131 | struct lttng_condition *condition; | |
132 | struct cds_list_head node; | |
133 | }; | |
134 | ||
ab0ee2ca JG |
135 | struct channel_state_sample { |
136 | struct channel_key key; | |
137 | struct cds_lfht_node channel_state_ht_node; | |
138 | uint64_t highest_usage; | |
139 | uint64_t lowest_usage; | |
e8360425 | 140 | uint64_t channel_total_consumed; |
83b934ad MD |
141 | /* call_rcu delayed reclaim. */ |
142 | struct rcu_head rcu_node; | |
ab0ee2ca JG |
143 | }; |
144 | ||
e4db5ace | 145 | static unsigned long hash_channel_key(struct channel_key *key); |
ed327204 | 146 | static int evaluate_buffer_condition(const struct lttng_condition *condition, |
e4db5ace | 147 | struct lttng_evaluation **evaluation, |
e8360425 JD |
148 | const struct notification_thread_state *state, |
149 | const struct channel_state_sample *previous_sample, | |
150 | const struct channel_state_sample *latest_sample, | |
151 | uint64_t previous_session_consumed_total, | |
152 | uint64_t latest_session_consumed_total, | |
153 | struct channel_info *channel_info); | |
e4db5ace | 154 | static |
9b63a4aa JG |
155 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, |
156 | const struct lttng_evaluation *evaluation, | |
e4db5ace JR |
157 | struct notification_client_list *client_list, |
158 | struct notification_thread_state *state, | |
159 | uid_t channel_uid, gid_t channel_gid); | |
160 | ||
8abe313a | 161 | |
ea9a44f0 | 162 | /* session_info API */ |
8abe313a JG |
163 | static |
164 | void session_info_destroy(void *_data); | |
165 | static | |
166 | void session_info_get(struct session_info *session_info); | |
167 | static | |
168 | void session_info_put(struct session_info *session_info); | |
169 | static | |
170 | struct session_info *session_info_create(const char *name, | |
ea9a44f0 | 171 | uid_t uid, gid_t gid, |
1eee26c5 JG |
172 | struct lttng_session_trigger_list *trigger_list, |
173 | struct cds_lfht *sessions_ht); | |
8abe313a JG |
174 | static |
175 | void session_info_add_channel(struct session_info *session_info, | |
176 | struct channel_info *channel_info); | |
177 | static | |
178 | void session_info_remove_channel(struct session_info *session_info, | |
179 | struct channel_info *channel_info); | |
180 | ||
ea9a44f0 JG |
181 | /* lttng_session_trigger_list API */ |
182 | static | |
183 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
184 | const char *session_name, | |
185 | struct cds_lfht *session_triggers_ht); | |
186 | static | |
187 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
188 | const struct notification_thread_state *state, | |
189 | const char *session_name); | |
190 | static | |
191 | void lttng_session_trigger_list_destroy( | |
192 | struct lttng_session_trigger_list *list); | |
193 | static | |
194 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
f2b3ef9f | 195 | struct lttng_trigger *trigger); |
ea9a44f0 | 196 | |
f2b3ef9f JG |
197 | static |
198 | int client_handle_transmission_status( | |
199 | struct notification_client *client, | |
200 | enum client_transmission_status transmission_status, | |
201 | struct notification_thread_state *state); | |
ea9a44f0 | 202 | |
8b524060 FD |
203 | static |
204 | int handle_one_event_notifier_notification( | |
205 | struct notification_thread_state *state, | |
206 | int pipe, enum lttng_domain_type domain); | |
207 | ||
242388e4 JR |
208 | static |
209 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node); | |
210 | ||
ab0ee2ca | 211 | static |
ac1889bf | 212 | int match_client_socket(struct cds_lfht_node *node, const void *key) |
ab0ee2ca JG |
213 | { |
214 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
ac1889bf JG |
215 | const int socket = (int) (intptr_t) key; |
216 | const struct notification_client *client = caa_container_of(node, | |
217 | struct notification_client, client_socket_ht_node); | |
ab0ee2ca | 218 | |
ac1889bf JG |
219 | return client->socket == socket; |
220 | } | |
221 | ||
222 | static | |
223 | int match_client_id(struct cds_lfht_node *node, const void *key) | |
224 | { | |
225 | /* This double-cast is intended to supress pointer-to-cast warning. */ | |
226 | const notification_client_id id = *((notification_client_id *) key); | |
227 | const struct notification_client *client = caa_container_of( | |
228 | node, struct notification_client, client_id_ht_node); | |
ab0ee2ca | 229 | |
ac1889bf | 230 | return client->id == id; |
ab0ee2ca JG |
231 | } |
232 | ||
233 | static | |
234 | int match_channel_trigger_list(struct cds_lfht_node *node, const void *key) | |
235 | { | |
236 | struct channel_key *channel_key = (struct channel_key *) key; | |
237 | struct lttng_channel_trigger_list *trigger_list; | |
238 | ||
239 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
240 | channel_triggers_ht_node); | |
241 | ||
242 | return !!((channel_key->key == trigger_list->channel_key.key) && | |
243 | (channel_key->domain == trigger_list->channel_key.domain)); | |
244 | } | |
245 | ||
51eab943 JG |
246 | static |
247 | int match_session_trigger_list(struct cds_lfht_node *node, const void *key) | |
248 | { | |
249 | const char *session_name = (const char *) key; | |
250 | struct lttng_session_trigger_list *trigger_list; | |
251 | ||
252 | trigger_list = caa_container_of(node, struct lttng_session_trigger_list, | |
253 | session_triggers_ht_node); | |
254 | ||
255 | return !!(strcmp(trigger_list->session_name, session_name) == 0); | |
256 | } | |
257 | ||
ab0ee2ca JG |
258 | static |
259 | int match_channel_state_sample(struct cds_lfht_node *node, const void *key) | |
260 | { | |
261 | struct channel_key *channel_key = (struct channel_key *) key; | |
262 | struct channel_state_sample *sample; | |
263 | ||
264 | sample = caa_container_of(node, struct channel_state_sample, | |
265 | channel_state_ht_node); | |
266 | ||
267 | return !!((channel_key->key == sample->key.key) && | |
268 | (channel_key->domain == sample->key.domain)); | |
269 | } | |
270 | ||
271 | static | |
272 | int match_channel_info(struct cds_lfht_node *node, const void *key) | |
273 | { | |
274 | struct channel_key *channel_key = (struct channel_key *) key; | |
275 | struct channel_info *channel_info; | |
276 | ||
277 | channel_info = caa_container_of(node, struct channel_info, | |
278 | channels_ht_node); | |
279 | ||
280 | return !!((channel_key->key == channel_info->key.key) && | |
281 | (channel_key->domain == channel_info->key.domain)); | |
282 | } | |
283 | ||
284 | static | |
242388e4 | 285 | int match_trigger(struct cds_lfht_node *node, const void *key) |
ab0ee2ca | 286 | { |
242388e4 JR |
287 | struct lttng_trigger *trigger_key = (struct lttng_trigger *) key; |
288 | struct lttng_trigger_ht_element *trigger_ht_element; | |
ab0ee2ca | 289 | |
242388e4 | 290 | trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element, |
ab0ee2ca | 291 | node); |
ab0ee2ca | 292 | |
242388e4 | 293 | return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger); |
ab0ee2ca JG |
294 | } |
295 | ||
e7c93cf9 JR |
296 | static |
297 | int match_trigger_token(struct cds_lfht_node *node, const void *key) | |
298 | { | |
299 | const uint64_t *_key = key; | |
300 | struct notification_trigger_tokens_ht_element *element; | |
301 | ||
302 | element = caa_container_of(node, | |
303 | struct notification_trigger_tokens_ht_element, node); | |
304 | return *_key == element->token; | |
305 | } | |
306 | ||
ab0ee2ca JG |
307 | static |
308 | int match_client_list_condition(struct cds_lfht_node *node, const void *key) | |
309 | { | |
310 | struct lttng_condition *condition_key = (struct lttng_condition *) key; | |
311 | struct notification_client_list *client_list; | |
f82f93a1 | 312 | const struct lttng_condition *condition; |
ab0ee2ca JG |
313 | |
314 | assert(condition_key); | |
315 | ||
316 | client_list = caa_container_of(node, struct notification_client_list, | |
505b2d90 | 317 | notification_trigger_clients_ht_node); |
091fa780 | 318 | condition = client_list->condition; |
ab0ee2ca JG |
319 | |
320 | return !!lttng_condition_is_equal(condition_key, condition); | |
321 | } | |
322 | ||
f82f93a1 JG |
323 | static |
324 | int match_session(struct cds_lfht_node *node, const void *key) | |
325 | { | |
326 | const char *name = key; | |
327 | struct session_info *session_info = caa_container_of( | |
328 | node, struct session_info, sessions_ht_node); | |
329 | ||
330 | return !strcmp(session_info->name, name); | |
331 | } | |
332 | ||
6900ae81 FD |
333 | static |
334 | const char *notification_command_type_str( | |
335 | enum notification_thread_command_type type) | |
336 | { | |
337 | switch (type) { | |
338 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
339 | return "REGISTER_TRIGGER"; | |
340 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
341 | return "UNREGISTER_TRIGGER"; | |
342 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
343 | return "ADD_CHANNEL"; | |
344 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
345 | return "REMOVE_CHANNEL"; | |
346 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: | |
347 | return "SESSION_ROTATION_ONGOING"; | |
348 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: | |
349 | return "SESSION_ROTATION_COMPLETED"; | |
350 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: | |
351 | return "ADD_TRACER_EVENT_SOURCE"; | |
352 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: | |
353 | return "REMOVE_TRACER_EVENT_SOURCE"; | |
354 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: | |
355 | return "LIST_TRIGGERS"; | |
8790759c FD |
356 | case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER: |
357 | return "GET_TRIGGER"; | |
6900ae81 FD |
358 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
359 | return "QUIT"; | |
360 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: | |
361 | return "CLIENT_COMMUNICATION_UPDATE"; | |
362 | default: | |
363 | abort(); | |
364 | } | |
365 | } | |
366 | ||
242388e4 JR |
367 | /* |
368 | * Match trigger based on name and credentials only. | |
369 | * Name duplication is NOT allowed for the same uid. | |
370 | */ | |
371 | static | |
372 | int match_trigger_by_name_uid(struct cds_lfht_node *node, | |
373 | const void *key) | |
374 | { | |
375 | bool match = false; | |
0efb2ad7 | 376 | const char *element_trigger_name; |
242388e4 JR |
377 | const char *key_name; |
378 | enum lttng_trigger_status status; | |
379 | const struct lttng_credentials *key_creds; | |
380 | const struct lttng_credentials *node_creds; | |
381 | const struct lttng_trigger *trigger_key = | |
382 | (const struct lttng_trigger *) key; | |
383 | const struct lttng_trigger_ht_element *trigger_ht_element = | |
384 | caa_container_of(node, | |
385 | struct lttng_trigger_ht_element, | |
386 | node_by_name_uid); | |
387 | ||
0efb2ad7 JG |
388 | status = lttng_trigger_get_name(trigger_ht_element->trigger, |
389 | &element_trigger_name); | |
390 | element_trigger_name = status == LTTNG_TRIGGER_STATUS_OK ? | |
391 | element_trigger_name : NULL; | |
242388e4 JR |
392 | |
393 | status = lttng_trigger_get_name(trigger_key, &key_name); | |
0efb2ad7 | 394 | key_name = status == LTTNG_TRIGGER_STATUS_OK ? key_name : NULL; |
242388e4 | 395 | |
0efb2ad7 JG |
396 | /* |
397 | * Compare the names. | |
398 | * Consider null names as not equal. This is to maintain backwards | |
399 | * compatibility with pre-2.13 anonymous triggers. Multiples anonymous | |
400 | * triggers are allowed for a given user. | |
401 | */ | |
402 | if (!element_trigger_name || !key_name) { | |
403 | goto end; | |
404 | } | |
405 | ||
406 | if (strcmp(element_trigger_name, key_name) != 0) { | |
242388e4 JR |
407 | goto end; |
408 | } | |
409 | ||
410 | /* Compare the owners' UIDs. */ | |
411 | key_creds = lttng_trigger_get_credentials(trigger_key); | |
412 | node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
413 | ||
414 | match = lttng_credentials_is_equal_uid(key_creds, node_creds); | |
415 | ||
416 | end: | |
417 | return match; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Hash trigger based on name and credentials only. | |
422 | */ | |
423 | static | |
424 | unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger) | |
425 | { | |
426 | unsigned long hash = 0; | |
427 | const struct lttng_credentials *trigger_creds; | |
428 | const char *trigger_name; | |
429 | enum lttng_trigger_status status; | |
430 | ||
431 | status = lttng_trigger_get_name(trigger, &trigger_name); | |
432 | if (status == LTTNG_TRIGGER_STATUS_OK) { | |
433 | hash = hash_key_str(trigger_name, lttng_ht_seed); | |
434 | } | |
435 | ||
436 | trigger_creds = lttng_trigger_get_credentials(trigger); | |
437 | hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid), | |
438 | lttng_ht_seed); | |
439 | ||
440 | return hash; | |
441 | } | |
442 | ||
e4db5ace JR |
443 | static |
444 | unsigned long hash_channel_key(struct channel_key *key) | |
445 | { | |
446 | unsigned long key_hash = hash_key_u64(&key->key, lttng_ht_seed); | |
447 | unsigned long domain_hash = hash_key_ulong( | |
448 | (void *) (unsigned long) key->domain, lttng_ht_seed); | |
449 | ||
450 | return key_hash ^ domain_hash; | |
451 | } | |
452 | ||
ac1889bf JG |
453 | static |
454 | unsigned long hash_client_socket(int socket) | |
455 | { | |
456 | return hash_key_ulong((void *) (unsigned long) socket, lttng_ht_seed); | |
457 | } | |
458 | ||
459 | static | |
460 | unsigned long hash_client_id(notification_client_id id) | |
461 | { | |
462 | return hash_key_u64(&id, lttng_ht_seed); | |
463 | } | |
464 | ||
f82f93a1 JG |
465 | /* |
466 | * Get the type of object to which a given condition applies. Bindings let | |
467 | * the notification system evaluate a trigger's condition when a given | |
468 | * object's state is updated. | |
469 | * | |
470 | * For instance, a condition bound to a channel will be evaluated everytime | |
471 | * the channel's state is changed by a channel monitoring sample. | |
472 | */ | |
473 | static | |
474 | enum lttng_object_type get_condition_binding_object( | |
475 | const struct lttng_condition *condition) | |
476 | { | |
477 | switch (lttng_condition_get_type(condition)) { | |
478 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
479 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
480 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
e742b055 | 481 | return LTTNG_OBJECT_TYPE_CHANNEL; |
f82f93a1 JG |
482 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
483 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
484 | return LTTNG_OBJECT_TYPE_SESSION; | |
8dbb86b8 | 485 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
959e3c66 | 486 | return LTTNG_OBJECT_TYPE_NONE; |
f82f93a1 JG |
487 | default: |
488 | return LTTNG_OBJECT_TYPE_UNKNOWN; | |
489 | } | |
490 | } | |
491 | ||
83b934ad MD |
492 | static |
493 | void free_channel_info_rcu(struct rcu_head *node) | |
494 | { | |
495 | free(caa_container_of(node, struct channel_info, rcu_node)); | |
496 | } | |
497 | ||
ab0ee2ca JG |
498 | static |
499 | void channel_info_destroy(struct channel_info *channel_info) | |
500 | { | |
501 | if (!channel_info) { | |
502 | return; | |
503 | } | |
504 | ||
8abe313a JG |
505 | if (channel_info->session_info) { |
506 | session_info_remove_channel(channel_info->session_info, | |
507 | channel_info); | |
508 | session_info_put(channel_info->session_info); | |
ab0ee2ca | 509 | } |
8abe313a JG |
510 | if (channel_info->name) { |
511 | free(channel_info->name); | |
ab0ee2ca | 512 | } |
83b934ad MD |
513 | call_rcu(&channel_info->rcu_node, free_channel_info_rcu); |
514 | } | |
515 | ||
516 | static | |
517 | void free_session_info_rcu(struct rcu_head *node) | |
518 | { | |
519 | free(caa_container_of(node, struct session_info, rcu_node)); | |
ab0ee2ca JG |
520 | } |
521 | ||
8abe313a | 522 | /* Don't call directly, use the ref-counting mechanism. */ |
ab0ee2ca | 523 | static |
8abe313a | 524 | void session_info_destroy(void *_data) |
ab0ee2ca | 525 | { |
8abe313a | 526 | struct session_info *session_info = _data; |
3b68d0a3 | 527 | int ret; |
ab0ee2ca | 528 | |
8abe313a JG |
529 | assert(session_info); |
530 | if (session_info->channel_infos_ht) { | |
3b68d0a3 JR |
531 | ret = cds_lfht_destroy(session_info->channel_infos_ht, NULL); |
532 | if (ret) { | |
4c3f302b | 533 | ERR("[notification-thread] Failed to destroy channel information hash table"); |
3b68d0a3 | 534 | } |
8abe313a | 535 | } |
ea9a44f0 | 536 | lttng_session_trigger_list_destroy(session_info->trigger_list); |
1eee26c5 JG |
537 | |
538 | rcu_read_lock(); | |
539 | cds_lfht_del(session_info->sessions_ht, | |
540 | &session_info->sessions_ht_node); | |
541 | rcu_read_unlock(); | |
8abe313a | 542 | free(session_info->name); |
83b934ad | 543 | call_rcu(&session_info->rcu_node, free_session_info_rcu); |
8abe313a | 544 | } |
ab0ee2ca | 545 | |
8abe313a JG |
546 | static |
547 | void session_info_get(struct session_info *session_info) | |
548 | { | |
549 | if (!session_info) { | |
550 | return; | |
551 | } | |
552 | lttng_ref_get(&session_info->ref); | |
553 | } | |
554 | ||
555 | static | |
556 | void session_info_put(struct session_info *session_info) | |
557 | { | |
558 | if (!session_info) { | |
559 | return; | |
ab0ee2ca | 560 | } |
8abe313a JG |
561 | lttng_ref_put(&session_info->ref); |
562 | } | |
563 | ||
564 | static | |
ea9a44f0 | 565 | struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid, |
1eee26c5 JG |
566 | struct lttng_session_trigger_list *trigger_list, |
567 | struct cds_lfht *sessions_ht) | |
8abe313a JG |
568 | { |
569 | struct session_info *session_info; | |
ab0ee2ca | 570 | |
8abe313a | 571 | assert(name); |
ab0ee2ca | 572 | |
8abe313a JG |
573 | session_info = zmalloc(sizeof(*session_info)); |
574 | if (!session_info) { | |
575 | goto end; | |
576 | } | |
577 | lttng_ref_init(&session_info->ref, session_info_destroy); | |
578 | ||
579 | session_info->channel_infos_ht = cds_lfht_new(DEFAULT_HT_SIZE, | |
580 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); | |
581 | if (!session_info->channel_infos_ht) { | |
ab0ee2ca JG |
582 | goto error; |
583 | } | |
8abe313a JG |
584 | |
585 | cds_lfht_node_init(&session_info->sessions_ht_node); | |
586 | session_info->name = strdup(name); | |
587 | if (!session_info->name) { | |
ab0ee2ca JG |
588 | goto error; |
589 | } | |
8abe313a JG |
590 | session_info->uid = uid; |
591 | session_info->gid = gid; | |
ea9a44f0 | 592 | session_info->trigger_list = trigger_list; |
1eee26c5 | 593 | session_info->sessions_ht = sessions_ht; |
8abe313a JG |
594 | end: |
595 | return session_info; | |
596 | error: | |
597 | session_info_put(session_info); | |
598 | return NULL; | |
599 | } | |
600 | ||
601 | static | |
602 | void session_info_add_channel(struct session_info *session_info, | |
603 | struct channel_info *channel_info) | |
604 | { | |
605 | rcu_read_lock(); | |
606 | cds_lfht_add(session_info->channel_infos_ht, | |
607 | hash_channel_key(&channel_info->key), | |
608 | &channel_info->session_info_channels_ht_node); | |
609 | rcu_read_unlock(); | |
610 | } | |
611 | ||
612 | static | |
613 | void session_info_remove_channel(struct session_info *session_info, | |
614 | struct channel_info *channel_info) | |
615 | { | |
616 | rcu_read_lock(); | |
617 | cds_lfht_del(session_info->channel_infos_ht, | |
618 | &channel_info->session_info_channels_ht_node); | |
619 | rcu_read_unlock(); | |
620 | } | |
621 | ||
622 | static | |
623 | struct channel_info *channel_info_create(const char *channel_name, | |
624 | struct channel_key *channel_key, uint64_t channel_capacity, | |
625 | struct session_info *session_info) | |
626 | { | |
627 | struct channel_info *channel_info = zmalloc(sizeof(*channel_info)); | |
628 | ||
629 | if (!channel_info) { | |
630 | goto end; | |
631 | } | |
632 | ||
ab0ee2ca | 633 | cds_lfht_node_init(&channel_info->channels_ht_node); |
8abe313a JG |
634 | cds_lfht_node_init(&channel_info->session_info_channels_ht_node); |
635 | memcpy(&channel_info->key, channel_key, sizeof(*channel_key)); | |
636 | channel_info->capacity = channel_capacity; | |
637 | ||
638 | channel_info->name = strdup(channel_name); | |
639 | if (!channel_info->name) { | |
640 | goto error; | |
641 | } | |
642 | ||
643 | /* | |
644 | * Set the references between session and channel infos: | |
645 | * - channel_info holds a strong reference to session_info | |
646 | * - session_info holds a weak reference to channel_info | |
647 | */ | |
648 | session_info_get(session_info); | |
649 | session_info_add_channel(session_info, channel_info); | |
650 | channel_info->session_info = session_info; | |
ab0ee2ca | 651 | end: |
8abe313a | 652 | return channel_info; |
ab0ee2ca | 653 | error: |
8abe313a | 654 | channel_info_destroy(channel_info); |
ab0ee2ca JG |
655 | return NULL; |
656 | } | |
657 | ||
f2b3ef9f | 658 | LTTNG_HIDDEN |
505b2d90 JG |
659 | bool notification_client_list_get(struct notification_client_list *list) |
660 | { | |
661 | return urcu_ref_get_unless_zero(&list->ref); | |
662 | } | |
663 | ||
664 | static | |
665 | void free_notification_client_list_rcu(struct rcu_head *node) | |
666 | { | |
667 | free(caa_container_of(node, struct notification_client_list, | |
668 | rcu_node)); | |
669 | } | |
670 | ||
671 | static | |
672 | void notification_client_list_release(struct urcu_ref *list_ref) | |
673 | { | |
674 | struct notification_client_list *list = | |
675 | container_of(list_ref, typeof(*list), ref); | |
676 | struct notification_client_list_element *client_list_element, *tmp; | |
677 | ||
091fa780 FD |
678 | lttng_condition_put(list->condition); |
679 | ||
505b2d90 JG |
680 | if (list->notification_trigger_clients_ht) { |
681 | rcu_read_lock(); | |
091fa780 | 682 | |
505b2d90 JG |
683 | cds_lfht_del(list->notification_trigger_clients_ht, |
684 | &list->notification_trigger_clients_ht_node); | |
685 | rcu_read_unlock(); | |
686 | list->notification_trigger_clients_ht = NULL; | |
687 | } | |
688 | cds_list_for_each_entry_safe(client_list_element, tmp, | |
091fa780 | 689 | &list->clients_list, node) { |
505b2d90 JG |
690 | free(client_list_element); |
691 | } | |
091fa780 FD |
692 | |
693 | assert(cds_list_empty(&list->triggers_list)); | |
694 | ||
505b2d90 JG |
695 | pthread_mutex_destroy(&list->lock); |
696 | call_rcu(&list->rcu_node, free_notification_client_list_rcu); | |
697 | } | |
698 | ||
091fa780 FD |
699 | static |
700 | bool condition_applies_to_client(const struct lttng_condition *condition, | |
701 | struct notification_client *client) | |
702 | { | |
703 | bool applies = false; | |
704 | struct lttng_condition_list_element *condition_list_element; | |
705 | ||
706 | cds_list_for_each_entry(condition_list_element, &client->condition_list, | |
707 | node) { | |
708 | applies = lttng_condition_is_equal( | |
709 | condition_list_element->condition, | |
710 | condition); | |
711 | if (applies) { | |
712 | break; | |
713 | } | |
714 | } | |
715 | ||
716 | return applies; | |
717 | } | |
718 | ||
505b2d90 JG |
719 | static |
720 | struct notification_client_list *notification_client_list_create( | |
091fa780 FD |
721 | struct notification_thread_state *state, |
722 | const struct lttng_condition *condition) | |
505b2d90 | 723 | { |
091fa780 FD |
724 | struct notification_client *client; |
725 | struct cds_lfht_iter iter; | |
726 | struct notification_client_list *client_list; | |
505b2d90 | 727 | |
091fa780 | 728 | client_list = zmalloc(sizeof(*client_list)); |
505b2d90 | 729 | if (!client_list) { |
091fa780 FD |
730 | PERROR("Failed to allocate notification client list"); |
731 | goto end; | |
505b2d90 | 732 | } |
091fa780 | 733 | |
505b2d90 | 734 | pthread_mutex_init(&client_list->lock, NULL); |
091fa780 FD |
735 | /* |
736 | * The trigger that owns the condition has the first reference to this | |
737 | * client list. | |
738 | */ | |
505b2d90 JG |
739 | urcu_ref_init(&client_list->ref); |
740 | cds_lfht_node_init(&client_list->notification_trigger_clients_ht_node); | |
091fa780 FD |
741 | CDS_INIT_LIST_HEAD(&client_list->clients_list); |
742 | CDS_INIT_LIST_HEAD(&client_list->triggers_list); | |
505b2d90 | 743 | |
091fa780 FD |
744 | /* |
745 | * Create a copy of the condition so that it's independent of any | |
746 | * trigger. The client list may outlive the trigger object (which owns | |
747 | * the condition) that is used to create it. | |
748 | */ | |
749 | client_list->condition = lttng_condition_copy(condition); | |
750 | ||
751 | /* Build a list of clients to which this new condition applies. */ | |
752 | cds_lfht_for_each_entry (state->client_socket_ht, &iter, client, | |
753 | client_socket_ht_node) { | |
754 | struct notification_client_list_element *client_list_element; | |
505b2d90 | 755 | |
091fa780 FD |
756 | if (!condition_applies_to_client(condition, client)) { |
757 | continue; | |
758 | } | |
759 | ||
760 | client_list_element = zmalloc(sizeof(*client_list_element)); | |
761 | if (!client_list_element) { | |
762 | goto error_put_client_list; | |
763 | } | |
505b2d90 | 764 | |
091fa780 FD |
765 | CDS_INIT_LIST_HEAD(&client_list_element->node); |
766 | client_list_element->client = client; | |
767 | cds_list_add(&client_list_element->node, &client_list->clients_list); | |
768 | } | |
769 | ||
770 | client_list->notification_trigger_clients_ht = | |
505b2d90 JG |
771 | state->notification_trigger_clients_ht; |
772 | ||
773 | rcu_read_lock(); | |
091fa780 FD |
774 | /* |
775 | * Add the client list to the global list of client list. | |
776 | */ | |
777 | cds_lfht_add_unique(state->notification_trigger_clients_ht, | |
778 | lttng_condition_hash(client_list->condition), | |
779 | match_client_list_condition, | |
780 | client_list->condition, | |
781 | &client_list->notification_trigger_clients_ht_node); | |
505b2d90 | 782 | rcu_read_unlock(); |
091fa780 FD |
783 | goto end; |
784 | ||
785 | error_put_client_list: | |
786 | notification_client_list_put(client_list); | |
787 | client_list = NULL; | |
788 | ||
789 | end: | |
790 | return client_list; | |
505b2d90 JG |
791 | } |
792 | ||
505b2d90 JG |
793 | void notification_client_list_put(struct notification_client_list *list) |
794 | { | |
795 | if (!list) { | |
796 | return; | |
797 | } | |
798 | return urcu_ref_put(&list->ref, notification_client_list_release); | |
799 | } | |
800 | ||
801 | /* Provides a reference to the returned list. */ | |
ed327204 JG |
802 | static |
803 | struct notification_client_list *get_client_list_from_condition( | |
804 | struct notification_thread_state *state, | |
805 | const struct lttng_condition *condition) | |
806 | { | |
807 | struct cds_lfht_node *node; | |
808 | struct cds_lfht_iter iter; | |
505b2d90 | 809 | struct notification_client_list *list = NULL; |
ed327204 | 810 | |
505b2d90 | 811 | rcu_read_lock(); |
ed327204 JG |
812 | cds_lfht_lookup(state->notification_trigger_clients_ht, |
813 | lttng_condition_hash(condition), | |
814 | match_client_list_condition, | |
815 | condition, | |
816 | &iter); | |
817 | node = cds_lfht_iter_get_node(&iter); | |
505b2d90 JG |
818 | if (node) { |
819 | list = container_of(node, struct notification_client_list, | |
820 | notification_trigger_clients_ht_node); | |
821 | list = notification_client_list_get(list) ? list : NULL; | |
822 | } | |
ed327204 | 823 | |
505b2d90 JG |
824 | rcu_read_unlock(); |
825 | return list; | |
ed327204 JG |
826 | } |
827 | ||
e4db5ace | 828 | static |
f82f93a1 JG |
829 | int evaluate_channel_condition_for_client( |
830 | const struct lttng_condition *condition, | |
831 | struct notification_thread_state *state, | |
832 | struct lttng_evaluation **evaluation, | |
833 | uid_t *session_uid, gid_t *session_gid) | |
e4db5ace JR |
834 | { |
835 | int ret; | |
836 | struct cds_lfht_iter iter; | |
837 | struct cds_lfht_node *node; | |
838 | struct channel_info *channel_info = NULL; | |
839 | struct channel_key *channel_key = NULL; | |
840 | struct channel_state_sample *last_sample = NULL; | |
841 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; | |
e4db5ace | 842 | |
505b2d90 JG |
843 | rcu_read_lock(); |
844 | ||
f82f93a1 | 845 | /* Find the channel associated with the condition. */ |
e4db5ace | 846 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, |
f82f93a1 | 847 | channel_trigger_list, channel_triggers_ht_node) { |
e4db5ace JR |
848 | struct lttng_trigger_list_element *element; |
849 | ||
850 | cds_list_for_each_entry(element, &channel_trigger_list->list, node) { | |
9b63a4aa | 851 | const struct lttng_condition *current_condition = |
f82f93a1 | 852 | lttng_trigger_get_const_condition( |
e4db5ace JR |
853 | element->trigger); |
854 | ||
855 | assert(current_condition); | |
856 | if (!lttng_condition_is_equal(condition, | |
2ae99f0b | 857 | current_condition)) { |
e4db5ace JR |
858 | continue; |
859 | } | |
860 | ||
861 | /* Found the trigger, save the channel key. */ | |
862 | channel_key = &channel_trigger_list->channel_key; | |
863 | break; | |
864 | } | |
865 | if (channel_key) { | |
866 | /* The channel key was found stop iteration. */ | |
867 | break; | |
868 | } | |
869 | } | |
870 | ||
871 | if (!channel_key){ | |
872 | /* No channel found; normal exit. */ | |
f82f93a1 | 873 | DBG("[notification-thread] No known channel associated with newly subscribed-to condition"); |
e4db5ace JR |
874 | ret = 0; |
875 | goto end; | |
876 | } | |
877 | ||
878 | /* Fetch channel info for the matching channel. */ | |
879 | cds_lfht_lookup(state->channels_ht, | |
880 | hash_channel_key(channel_key), | |
881 | match_channel_info, | |
882 | channel_key, | |
883 | &iter); | |
884 | node = cds_lfht_iter_get_node(&iter); | |
885 | assert(node); | |
886 | channel_info = caa_container_of(node, struct channel_info, | |
887 | channels_ht_node); | |
888 | ||
889 | /* Retrieve the channel's last sample, if it exists. */ | |
890 | cds_lfht_lookup(state->channel_state_ht, | |
891 | hash_channel_key(channel_key), | |
892 | match_channel_state_sample, | |
893 | channel_key, | |
894 | &iter); | |
895 | node = cds_lfht_iter_get_node(&iter); | |
896 | if (node) { | |
897 | last_sample = caa_container_of(node, | |
898 | struct channel_state_sample, | |
899 | channel_state_ht_node); | |
900 | } else { | |
901 | /* Nothing to evaluate, no sample was ever taken. Normal exit */ | |
902 | DBG("[notification-thread] No channel sample associated with newly subscribed-to condition"); | |
903 | ret = 0; | |
904 | goto end; | |
905 | } | |
906 | ||
f82f93a1 | 907 | ret = evaluate_buffer_condition(condition, evaluation, state, |
e8360425 JD |
908 | NULL, last_sample, |
909 | 0, channel_info->session_info->consumed_data_size, | |
910 | channel_info); | |
e4db5ace | 911 | if (ret) { |
066a3c82 | 912 | WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition"); |
e4db5ace JR |
913 | goto end; |
914 | } | |
915 | ||
f82f93a1 JG |
916 | *session_uid = channel_info->session_info->uid; |
917 | *session_gid = channel_info->session_info->gid; | |
918 | end: | |
505b2d90 | 919 | rcu_read_unlock(); |
f82f93a1 JG |
920 | return ret; |
921 | } | |
922 | ||
923 | static | |
924 | const char *get_condition_session_name(const struct lttng_condition *condition) | |
925 | { | |
926 | const char *session_name = NULL; | |
927 | enum lttng_condition_status status; | |
928 | ||
929 | switch (lttng_condition_get_type(condition)) { | |
930 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
931 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
932 | status = lttng_condition_buffer_usage_get_session_name( | |
933 | condition, &session_name); | |
934 | break; | |
935 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
936 | status = lttng_condition_session_consumed_size_get_session_name( | |
937 | condition, &session_name); | |
938 | break; | |
939 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
940 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
941 | status = lttng_condition_session_rotation_get_session_name( | |
942 | condition, &session_name); | |
943 | break; | |
944 | default: | |
945 | abort(); | |
946 | } | |
947 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
948 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
949 | goto end; | |
950 | } | |
951 | end: | |
952 | return session_name; | |
953 | } | |
954 | ||
f82f93a1 JG |
955 | static |
956 | int evaluate_session_condition_for_client( | |
957 | const struct lttng_condition *condition, | |
958 | struct notification_thread_state *state, | |
959 | struct lttng_evaluation **evaluation, | |
960 | uid_t *session_uid, gid_t *session_gid) | |
961 | { | |
962 | int ret; | |
963 | struct cds_lfht_iter iter; | |
964 | struct cds_lfht_node *node; | |
965 | const char *session_name; | |
966 | struct session_info *session_info = NULL; | |
967 | ||
505b2d90 | 968 | rcu_read_lock(); |
f82f93a1 JG |
969 | session_name = get_condition_session_name(condition); |
970 | ||
971 | /* Find the session associated with the trigger. */ | |
972 | cds_lfht_lookup(state->sessions_ht, | |
973 | hash_key_str(session_name, lttng_ht_seed), | |
974 | match_session, | |
975 | session_name, | |
976 | &iter); | |
977 | node = cds_lfht_iter_get_node(&iter); | |
978 | if (!node) { | |
979 | DBG("[notification-thread] No known session matching name \"%s\"", | |
980 | session_name); | |
981 | ret = 0; | |
982 | goto end; | |
983 | } | |
984 | ||
985 | session_info = caa_container_of(node, struct session_info, | |
986 | sessions_ht_node); | |
987 | session_info_get(session_info); | |
988 | ||
989 | /* | |
990 | * Evaluation is performed in-line here since only one type of | |
991 | * session-bound condition is handled for the moment. | |
992 | */ | |
993 | switch (lttng_condition_get_type(condition)) { | |
994 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
995 | if (!session_info->rotation.ongoing) { | |
be558f88 | 996 | ret = 0; |
f82f93a1 JG |
997 | goto end_session_put; |
998 | } | |
999 | ||
1000 | *evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
1001 | session_info->rotation.id); | |
1002 | if (!*evaluation) { | |
1003 | /* Fatal error. */ | |
1004 | ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"", | |
1005 | session_info->name); | |
1006 | ret = -1; | |
1007 | goto end_session_put; | |
1008 | } | |
1009 | ret = 0; | |
1010 | break; | |
1011 | default: | |
1012 | ret = 0; | |
1013 | goto end_session_put; | |
1014 | } | |
1015 | ||
1016 | *session_uid = session_info->uid; | |
1017 | *session_gid = session_info->gid; | |
1018 | ||
1019 | end_session_put: | |
1020 | session_info_put(session_info); | |
1021 | end: | |
505b2d90 | 1022 | rcu_read_unlock(); |
f82f93a1 JG |
1023 | return ret; |
1024 | } | |
1025 | ||
f82f93a1 JG |
1026 | static |
1027 | int evaluate_condition_for_client(const struct lttng_trigger *trigger, | |
1028 | const struct lttng_condition *condition, | |
1029 | struct notification_client *client, | |
1030 | struct notification_thread_state *state) | |
1031 | { | |
1032 | int ret; | |
1033 | struct lttng_evaluation *evaluation = NULL; | |
505b2d90 JG |
1034 | struct notification_client_list client_list = { |
1035 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
1036 | }; | |
f82f93a1 JG |
1037 | struct notification_client_list_element client_list_element = { 0 }; |
1038 | uid_t object_uid = 0; | |
1039 | gid_t object_gid = 0; | |
1040 | ||
1041 | assert(trigger); | |
1042 | assert(condition); | |
1043 | assert(client); | |
1044 | assert(state); | |
1045 | ||
1046 | switch (get_condition_binding_object(condition)) { | |
1047 | case LTTNG_OBJECT_TYPE_SESSION: | |
1048 | ret = evaluate_session_condition_for_client(condition, state, | |
1049 | &evaluation, &object_uid, &object_gid); | |
1050 | break; | |
1051 | case LTTNG_OBJECT_TYPE_CHANNEL: | |
1052 | ret = evaluate_channel_condition_for_client(condition, state, | |
1053 | &evaluation, &object_uid, &object_gid); | |
1054 | break; | |
1055 | case LTTNG_OBJECT_TYPE_NONE: | |
7e802d0a | 1056 | DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate"); |
f82f93a1 JG |
1057 | ret = 0; |
1058 | goto end; | |
1059 | case LTTNG_OBJECT_TYPE_UNKNOWN: | |
1060 | default: | |
1061 | ret = -1; | |
1062 | goto end; | |
1063 | } | |
af0c318d JG |
1064 | if (ret) { |
1065 | /* Fatal error. */ | |
1066 | goto end; | |
1067 | } | |
e4db5ace JR |
1068 | if (!evaluation) { |
1069 | /* Evaluation yielded nothing. Normal exit. */ | |
1070 | DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client"); | |
1071 | ret = 0; | |
1072 | goto end; | |
1073 | } | |
1074 | ||
1075 | /* | |
1076 | * Create a temporary client list with the client currently | |
1077 | * subscribing. | |
1078 | */ | |
505b2d90 | 1079 | cds_lfht_node_init(&client_list.notification_trigger_clients_ht_node); |
091fa780 | 1080 | CDS_INIT_LIST_HEAD(&client_list.clients_list); |
e4db5ace JR |
1081 | |
1082 | CDS_INIT_LIST_HEAD(&client_list_element.node); | |
1083 | client_list_element.client = client; | |
091fa780 | 1084 | cds_list_add(&client_list_element.node, &client_list.clients_list); |
e4db5ace JR |
1085 | |
1086 | /* Send evaluation result to the newly-subscribed client. */ | |
1087 | DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client"); | |
1088 | ret = send_evaluation_to_clients(trigger, evaluation, &client_list, | |
f82f93a1 | 1089 | state, object_uid, object_gid); |
e4db5ace JR |
1090 | |
1091 | end: | |
1092 | return ret; | |
1093 | } | |
1094 | ||
ab0ee2ca JG |
1095 | static |
1096 | int notification_thread_client_subscribe(struct notification_client *client, | |
1097 | struct lttng_condition *condition, | |
1098 | struct notification_thread_state *state, | |
1099 | enum lttng_notification_channel_status *_status) | |
1100 | { | |
1101 | int ret = 0; | |
505b2d90 | 1102 | struct notification_client_list *client_list = NULL; |
ab0ee2ca JG |
1103 | struct lttng_condition_list_element *condition_list_element = NULL; |
1104 | struct notification_client_list_element *client_list_element = NULL; | |
a3ed2a4e | 1105 | struct lttng_trigger_ht_element *trigger_ht_element; |
ab0ee2ca JG |
1106 | enum lttng_notification_channel_status status = |
1107 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
1108 | ||
1109 | /* | |
1110 | * Ensure that the client has not already subscribed to this condition | |
1111 | * before. | |
1112 | */ | |
1113 | cds_list_for_each_entry(condition_list_element, &client->condition_list, node) { | |
1114 | if (lttng_condition_is_equal(condition_list_element->condition, | |
1115 | condition)) { | |
1116 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED; | |
1117 | goto end; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | condition_list_element = zmalloc(sizeof(*condition_list_element)); | |
1122 | if (!condition_list_element) { | |
1123 | ret = -1; | |
1124 | goto error; | |
1125 | } | |
1126 | client_list_element = zmalloc(sizeof(*client_list_element)); | |
1127 | if (!client_list_element) { | |
1128 | ret = -1; | |
1129 | goto error; | |
1130 | } | |
1131 | ||
ab0ee2ca JG |
1132 | /* |
1133 | * Add the newly-subscribed condition to the client's subscription list. | |
1134 | */ | |
1135 | CDS_INIT_LIST_HEAD(&condition_list_element->node); | |
1136 | condition_list_element->condition = condition; | |
1137 | cds_list_add(&condition_list_element->node, &client->condition_list); | |
1138 | ||
ed327204 JG |
1139 | client_list = get_client_list_from_condition(state, condition); |
1140 | if (!client_list) { | |
2ae99f0b JG |
1141 | /* |
1142 | * No notification-emiting trigger registered with this | |
1143 | * condition. We don't evaluate the condition right away | |
1144 | * since this trigger is not registered yet. | |
1145 | */ | |
4fb43b68 | 1146 | free(client_list_element); |
505b2d90 | 1147 | goto end; |
ab0ee2ca JG |
1148 | } |
1149 | ||
2ae99f0b JG |
1150 | /* |
1151 | * The condition to which the client just subscribed is evaluated | |
1152 | * at this point so that conditions that are already TRUE result | |
1153 | * in a notification being sent out. | |
505b2d90 | 1154 | * |
57644a7f JG |
1155 | * Note the iteration on all triggers which share an identical |
1156 | * `condition` than the one to which the client is registering. This is | |
1157 | * done to ensure that the client receives a distinct notification for | |
1158 | * all triggers that have a `notify` action that have this condition. | |
2ae99f0b | 1159 | */ |
091fa780 FD |
1160 | pthread_mutex_lock(&client_list->lock); |
1161 | cds_list_for_each_entry(trigger_ht_element, | |
1162 | &client_list->triggers_list, client_list_trigger_node) { | |
1163 | if (evaluate_condition_for_client(trigger_ht_element->trigger, condition, | |
1164 | client, state)) { | |
1165 | WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting."); | |
1166 | ret = -1; | |
1167 | free(client_list_element); | |
ecc7ed07 | 1168 | pthread_mutex_unlock(&client_list->lock); |
091fa780 FD |
1169 | goto end; |
1170 | } | |
e4db5ace | 1171 | } |
091fa780 | 1172 | pthread_mutex_unlock(&client_list->lock); |
e4db5ace JR |
1173 | |
1174 | /* | |
1175 | * Add the client to the list of clients interested in a given trigger | |
1176 | * if a "notification" trigger with a corresponding condition was | |
1177 | * added prior. | |
1178 | */ | |
ab0ee2ca JG |
1179 | client_list_element->client = client; |
1180 | CDS_INIT_LIST_HEAD(&client_list_element->node); | |
505b2d90 JG |
1181 | |
1182 | pthread_mutex_lock(&client_list->lock); | |
091fa780 | 1183 | cds_list_add(&client_list_element->node, &client_list->clients_list); |
505b2d90 | 1184 | pthread_mutex_unlock(&client_list->lock); |
ab0ee2ca JG |
1185 | end: |
1186 | if (_status) { | |
1187 | *_status = status; | |
1188 | } | |
505b2d90 JG |
1189 | if (client_list) { |
1190 | notification_client_list_put(client_list); | |
1191 | } | |
ab0ee2ca JG |
1192 | return ret; |
1193 | error: | |
1194 | free(condition_list_element); | |
1195 | free(client_list_element); | |
1196 | return ret; | |
1197 | } | |
1198 | ||
1199 | static | |
1200 | int notification_thread_client_unsubscribe( | |
1201 | struct notification_client *client, | |
1202 | struct lttng_condition *condition, | |
1203 | struct notification_thread_state *state, | |
1204 | enum lttng_notification_channel_status *_status) | |
1205 | { | |
ab0ee2ca JG |
1206 | struct notification_client_list *client_list; |
1207 | struct lttng_condition_list_element *condition_list_element, | |
1208 | *condition_tmp; | |
1209 | struct notification_client_list_element *client_list_element, | |
1210 | *client_tmp; | |
1211 | bool condition_found = false; | |
1212 | enum lttng_notification_channel_status status = | |
1213 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
1214 | ||
1215 | /* Remove the condition from the client's condition list. */ | |
1216 | cds_list_for_each_entry_safe(condition_list_element, condition_tmp, | |
1217 | &client->condition_list, node) { | |
1218 | if (!lttng_condition_is_equal(condition_list_element->condition, | |
1219 | condition)) { | |
1220 | continue; | |
1221 | } | |
1222 | ||
1223 | cds_list_del(&condition_list_element->node); | |
1224 | /* | |
1225 | * The caller may be iterating on the client's conditions to | |
1226 | * tear down a client's connection. In this case, the condition | |
1227 | * will be destroyed at the end. | |
1228 | */ | |
1229 | if (condition != condition_list_element->condition) { | |
1230 | lttng_condition_destroy( | |
1231 | condition_list_element->condition); | |
1232 | } | |
1233 | free(condition_list_element); | |
1234 | condition_found = true; | |
1235 | break; | |
1236 | } | |
1237 | ||
1238 | if (!condition_found) { | |
1239 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION; | |
1240 | goto end; | |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * Remove the client from the list of clients interested the trigger | |
1245 | * matching the condition. | |
1246 | */ | |
ed327204 JG |
1247 | client_list = get_client_list_from_condition(state, condition); |
1248 | if (!client_list) { | |
505b2d90 | 1249 | goto end; |
ab0ee2ca JG |
1250 | } |
1251 | ||
505b2d90 | 1252 | pthread_mutex_lock(&client_list->lock); |
ab0ee2ca | 1253 | cds_list_for_each_entry_safe(client_list_element, client_tmp, |
091fa780 | 1254 | &client_list->clients_list, node) { |
505b2d90 | 1255 | if (client_list_element->client->id != client->id) { |
ab0ee2ca JG |
1256 | continue; |
1257 | } | |
1258 | cds_list_del(&client_list_element->node); | |
1259 | free(client_list_element); | |
1260 | break; | |
1261 | } | |
505b2d90 JG |
1262 | pthread_mutex_unlock(&client_list->lock); |
1263 | notification_client_list_put(client_list); | |
1264 | client_list = NULL; | |
ab0ee2ca JG |
1265 | end: |
1266 | lttng_condition_destroy(condition); | |
1267 | if (_status) { | |
1268 | *_status = status; | |
1269 | } | |
1270 | return 0; | |
1271 | } | |
1272 | ||
83b934ad MD |
1273 | static |
1274 | void free_notification_client_rcu(struct rcu_head *node) | |
1275 | { | |
1276 | free(caa_container_of(node, struct notification_client, rcu_node)); | |
1277 | } | |
1278 | ||
ab0ee2ca JG |
1279 | static |
1280 | void notification_client_destroy(struct notification_client *client, | |
1281 | struct notification_thread_state *state) | |
1282 | { | |
ab0ee2ca JG |
1283 | if (!client) { |
1284 | return; | |
1285 | } | |
1286 | ||
505b2d90 JG |
1287 | /* |
1288 | * The client object is not reachable by other threads, no need to lock | |
1289 | * the client here. | |
1290 | */ | |
ab0ee2ca JG |
1291 | if (client->socket >= 0) { |
1292 | (void) lttcomm_close_unix_sock(client->socket); | |
ac1889bf | 1293 | client->socket = -1; |
ab0ee2ca | 1294 | } |
505b2d90 | 1295 | client->communication.active = false; |
882093ee JR |
1296 | lttng_payload_reset(&client->communication.inbound.payload); |
1297 | lttng_payload_reset(&client->communication.outbound.payload); | |
505b2d90 | 1298 | pthread_mutex_destroy(&client->lock); |
83b934ad | 1299 | call_rcu(&client->rcu_node, free_notification_client_rcu); |
ab0ee2ca JG |
1300 | } |
1301 | ||
1302 | /* | |
1303 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1304 | * client pointer). | |
1305 | */ | |
1306 | static | |
1307 | struct notification_client *get_client_from_socket(int socket, | |
1308 | struct notification_thread_state *state) | |
1309 | { | |
1310 | struct cds_lfht_iter iter; | |
1311 | struct cds_lfht_node *node; | |
1312 | struct notification_client *client = NULL; | |
1313 | ||
1314 | cds_lfht_lookup(state->client_socket_ht, | |
ac1889bf JG |
1315 | hash_client_socket(socket), |
1316 | match_client_socket, | |
ab0ee2ca JG |
1317 | (void *) (unsigned long) socket, |
1318 | &iter); | |
1319 | node = cds_lfht_iter_get_node(&iter); | |
1320 | if (!node) { | |
1321 | goto end; | |
1322 | } | |
1323 | ||
1324 | client = caa_container_of(node, struct notification_client, | |
1325 | client_socket_ht_node); | |
1326 | end: | |
1327 | return client; | |
1328 | } | |
1329 | ||
f2b3ef9f JG |
1330 | /* |
1331 | * Call with rcu_read_lock held (and hold for the lifetime of the returned | |
1332 | * client pointer). | |
1333 | */ | |
1334 | static | |
1335 | struct notification_client *get_client_from_id(notification_client_id id, | |
1336 | struct notification_thread_state *state) | |
1337 | { | |
1338 | struct cds_lfht_iter iter; | |
1339 | struct cds_lfht_node *node; | |
1340 | struct notification_client *client = NULL; | |
1341 | ||
1342 | cds_lfht_lookup(state->client_id_ht, | |
1343 | hash_client_id(id), | |
1344 | match_client_id, | |
1345 | &id, | |
1346 | &iter); | |
1347 | node = cds_lfht_iter_get_node(&iter); | |
1348 | if (!node) { | |
1349 | goto end; | |
1350 | } | |
1351 | ||
1352 | client = caa_container_of(node, struct notification_client, | |
1353 | client_id_ht_node); | |
1354 | end: | |
1355 | return client; | |
1356 | } | |
1357 | ||
ab0ee2ca | 1358 | static |
e8360425 | 1359 | bool buffer_usage_condition_applies_to_channel( |
51eab943 JG |
1360 | const struct lttng_condition *condition, |
1361 | const struct channel_info *channel_info) | |
ab0ee2ca JG |
1362 | { |
1363 | enum lttng_condition_status status; | |
e8360425 JD |
1364 | enum lttng_domain_type condition_domain; |
1365 | const char *condition_session_name = NULL; | |
1366 | const char *condition_channel_name = NULL; | |
ab0ee2ca | 1367 | |
e8360425 JD |
1368 | status = lttng_condition_buffer_usage_get_domain_type(condition, |
1369 | &condition_domain); | |
1370 | assert(status == LTTNG_CONDITION_STATUS_OK); | |
1371 | if (channel_info->key.domain != condition_domain) { | |
ab0ee2ca JG |
1372 | goto fail; |
1373 | } | |
1374 | ||
e8360425 JD |
1375 | status = lttng_condition_buffer_usage_get_session_name( |
1376 | condition, &condition_session_name); | |
1377 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1378 | ||
1379 | status = lttng_condition_buffer_usage_get_channel_name( | |
1380 | condition, &condition_channel_name); | |
1381 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_channel_name); | |
1382 | ||
1383 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
1384 | goto fail; | |
1385 | } | |
1386 | if (strcmp(channel_info->name, condition_channel_name)) { | |
ab0ee2ca JG |
1387 | goto fail; |
1388 | } | |
1389 | ||
e8360425 JD |
1390 | return true; |
1391 | fail: | |
1392 | return false; | |
1393 | } | |
1394 | ||
1395 | static | |
1396 | bool session_consumed_size_condition_applies_to_channel( | |
51eab943 JG |
1397 | const struct lttng_condition *condition, |
1398 | const struct channel_info *channel_info) | |
e8360425 JD |
1399 | { |
1400 | enum lttng_condition_status status; | |
1401 | const char *condition_session_name = NULL; | |
1402 | ||
1403 | status = lttng_condition_session_consumed_size_get_session_name( | |
1404 | condition, &condition_session_name); | |
1405 | assert((status == LTTNG_CONDITION_STATUS_OK) && condition_session_name); | |
1406 | ||
1407 | if (strcmp(channel_info->session_info->name, condition_session_name)) { | |
ab0ee2ca JG |
1408 | goto fail; |
1409 | } | |
1410 | ||
e8360425 JD |
1411 | return true; |
1412 | fail: | |
1413 | return false; | |
1414 | } | |
ab0ee2ca | 1415 | |
51eab943 JG |
1416 | static |
1417 | bool trigger_applies_to_channel(const struct lttng_trigger *trigger, | |
1418 | const struct channel_info *channel_info) | |
1419 | { | |
1420 | const struct lttng_condition *condition; | |
e8360425 | 1421 | bool trigger_applies; |
ab0ee2ca | 1422 | |
51eab943 | 1423 | condition = lttng_trigger_get_const_condition(trigger); |
e8360425 | 1424 | if (!condition) { |
ab0ee2ca JG |
1425 | goto fail; |
1426 | } | |
e8360425 JD |
1427 | |
1428 | switch (lttng_condition_get_type(condition)) { | |
1429 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
1430 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
1431 | trigger_applies = buffer_usage_condition_applies_to_channel( | |
1432 | condition, channel_info); | |
1433 | break; | |
1434 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
1435 | trigger_applies = session_consumed_size_condition_applies_to_channel( | |
1436 | condition, channel_info); | |
1437 | break; | |
1438 | default: | |
ab0ee2ca JG |
1439 | goto fail; |
1440 | } | |
1441 | ||
e8360425 | 1442 | return trigger_applies; |
ab0ee2ca JG |
1443 | fail: |
1444 | return false; | |
1445 | } | |
1446 | ||
ed327204 JG |
1447 | /* Must be called with RCU read lock held. */ |
1448 | static | |
1449 | struct lttng_session_trigger_list *get_session_trigger_list( | |
1450 | struct notification_thread_state *state, | |
1451 | const char *session_name) | |
1452 | { | |
1453 | struct lttng_session_trigger_list *list = NULL; | |
1454 | struct cds_lfht_node *node; | |
1455 | struct cds_lfht_iter iter; | |
1456 | ||
1457 | cds_lfht_lookup(state->session_triggers_ht, | |
1458 | hash_key_str(session_name, lttng_ht_seed), | |
1459 | match_session_trigger_list, | |
1460 | session_name, | |
1461 | &iter); | |
1462 | node = cds_lfht_iter_get_node(&iter); | |
1463 | if (!node) { | |
1464 | /* | |
1465 | * Not an error, the list of triggers applying to that session | |
1466 | * will be initialized when the session is created. | |
1467 | */ | |
1468 | DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system", | |
1469 | session_name); | |
1470 | goto end; | |
1471 | } | |
1472 | ||
e742b055 | 1473 | list = caa_container_of(node, |
ed327204 JG |
1474 | struct lttng_session_trigger_list, |
1475 | session_triggers_ht_node); | |
1476 | end: | |
1477 | return list; | |
1478 | } | |
1479 | ||
ea9a44f0 JG |
1480 | /* |
1481 | * Allocate an empty lttng_session_trigger_list for the session named | |
1482 | * 'session_name'. | |
1483 | * | |
1484 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1485 | * It is the caller's responsability to ensure the session name is alive | |
1486 | * for as long as this list is. | |
1487 | */ | |
1488 | static | |
1489 | struct lttng_session_trigger_list *lttng_session_trigger_list_create( | |
1490 | const char *session_name, | |
1491 | struct cds_lfht *session_triggers_ht) | |
1492 | { | |
1493 | struct lttng_session_trigger_list *list; | |
1494 | ||
1495 | list = zmalloc(sizeof(*list)); | |
1496 | if (!list) { | |
1497 | goto end; | |
1498 | } | |
1499 | list->session_name = session_name; | |
1500 | CDS_INIT_LIST_HEAD(&list->list); | |
1501 | cds_lfht_node_init(&list->session_triggers_ht_node); | |
1502 | list->session_triggers_ht = session_triggers_ht; | |
1503 | ||
1504 | rcu_read_lock(); | |
1505 | /* Publish the list through the session_triggers_ht. */ | |
1506 | cds_lfht_add(session_triggers_ht, | |
1507 | hash_key_str(session_name, lttng_ht_seed), | |
1508 | &list->session_triggers_ht_node); | |
1509 | rcu_read_unlock(); | |
1510 | end: | |
1511 | return list; | |
1512 | } | |
1513 | ||
1514 | static | |
1515 | void free_session_trigger_list_rcu(struct rcu_head *node) | |
1516 | { | |
1517 | free(caa_container_of(node, struct lttng_session_trigger_list, | |
1518 | rcu_node)); | |
1519 | } | |
1520 | ||
1521 | static | |
1522 | void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list *list) | |
1523 | { | |
1524 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1525 | ||
1526 | /* Empty the list element by element, and then free the list itself. */ | |
1527 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1528 | &list->list, node) { | |
1529 | cds_list_del(&trigger_list_element->node); | |
1530 | free(trigger_list_element); | |
1531 | } | |
1532 | rcu_read_lock(); | |
1533 | /* Unpublish the list from the session_triggers_ht. */ | |
1534 | cds_lfht_del(list->session_triggers_ht, | |
1535 | &list->session_triggers_ht_node); | |
1536 | rcu_read_unlock(); | |
1537 | call_rcu(&list->rcu_node, free_session_trigger_list_rcu); | |
1538 | } | |
1539 | ||
1540 | static | |
1541 | int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list, | |
f2b3ef9f | 1542 | struct lttng_trigger *trigger) |
ea9a44f0 JG |
1543 | { |
1544 | int ret = 0; | |
1545 | struct lttng_trigger_list_element *new_element = | |
1546 | zmalloc(sizeof(*new_element)); | |
1547 | ||
1548 | if (!new_element) { | |
1549 | ret = -1; | |
1550 | goto end; | |
1551 | } | |
1552 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1553 | new_element->trigger = trigger; | |
1554 | cds_list_add(&new_element->node, &list->list); | |
1555 | end: | |
1556 | return ret; | |
1557 | } | |
1558 | ||
1559 | static | |
1560 | bool trigger_applies_to_session(const struct lttng_trigger *trigger, | |
1561 | const char *session_name) | |
1562 | { | |
1563 | bool applies = false; | |
1564 | const struct lttng_condition *condition; | |
1565 | ||
1566 | condition = lttng_trigger_get_const_condition(trigger); | |
1567 | switch (lttng_condition_get_type(condition)) { | |
1568 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
1569 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
1570 | { | |
1571 | enum lttng_condition_status condition_status; | |
1572 | const char *condition_session_name; | |
1573 | ||
1574 | condition_status = lttng_condition_session_rotation_get_session_name( | |
1575 | condition, &condition_session_name); | |
1576 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
1577 | ERR("[notification-thread] Failed to retrieve session rotation condition's session name"); | |
1578 | goto end; | |
1579 | } | |
1580 | ||
1581 | assert(condition_session_name); | |
1582 | applies = !strcmp(condition_session_name, session_name); | |
1583 | break; | |
1584 | } | |
1585 | default: | |
1586 | goto end; | |
1587 | } | |
1588 | end: | |
1589 | return applies; | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Allocate and initialize an lttng_session_trigger_list which contains | |
1594 | * all triggers that apply to the session named 'session_name'. | |
1595 | * | |
1596 | * No ownership of 'session_name' is assumed by the session trigger list. | |
1597 | * It is the caller's responsability to ensure the session name is alive | |
1598 | * for as long as this list is. | |
1599 | */ | |
1600 | static | |
1601 | struct lttng_session_trigger_list *lttng_session_trigger_list_build( | |
1602 | const struct notification_thread_state *state, | |
1603 | const char *session_name) | |
1604 | { | |
1605 | int trigger_count = 0; | |
1606 | struct lttng_session_trigger_list *session_trigger_list = NULL; | |
1607 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1608 | struct cds_lfht_iter iter; | |
1609 | ||
1610 | session_trigger_list = lttng_session_trigger_list_create(session_name, | |
1611 | state->session_triggers_ht); | |
1612 | ||
1613 | /* Add all triggers applying to the session named 'session_name'. */ | |
1614 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1615 | node) { | |
1616 | int ret; | |
1617 | ||
1618 | if (!trigger_applies_to_session(trigger_ht_element->trigger, | |
1619 | session_name)) { | |
1620 | continue; | |
1621 | } | |
1622 | ||
1623 | ret = lttng_session_trigger_list_add(session_trigger_list, | |
1624 | trigger_ht_element->trigger); | |
1625 | if (ret) { | |
1626 | goto error; | |
1627 | } | |
1628 | ||
1629 | trigger_count++; | |
1630 | } | |
1631 | ||
1632 | DBG("[notification-thread] Found %i triggers that apply to newly created session", | |
1633 | trigger_count); | |
1634 | return session_trigger_list; | |
1635 | error: | |
1636 | lttng_session_trigger_list_destroy(session_trigger_list); | |
1637 | return NULL; | |
1638 | } | |
1639 | ||
8abe313a JG |
1640 | static |
1641 | struct session_info *find_or_create_session_info( | |
1642 | struct notification_thread_state *state, | |
1643 | const char *name, uid_t uid, gid_t gid) | |
1644 | { | |
1645 | struct session_info *session = NULL; | |
1646 | struct cds_lfht_node *node; | |
1647 | struct cds_lfht_iter iter; | |
ea9a44f0 | 1648 | struct lttng_session_trigger_list *trigger_list; |
8abe313a JG |
1649 | |
1650 | rcu_read_lock(); | |
1651 | cds_lfht_lookup(state->sessions_ht, | |
1652 | hash_key_str(name, lttng_ht_seed), | |
1653 | match_session, | |
1654 | name, | |
1655 | &iter); | |
1656 | node = cds_lfht_iter_get_node(&iter); | |
1657 | if (node) { | |
1658 | DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)", | |
1659 | name, uid, gid); | |
1660 | session = caa_container_of(node, struct session_info, | |
1661 | sessions_ht_node); | |
1662 | assert(session->uid == uid); | |
1663 | assert(session->gid == gid); | |
1eee26c5 JG |
1664 | session_info_get(session); |
1665 | goto end; | |
ea9a44f0 JG |
1666 | } |
1667 | ||
1668 | trigger_list = lttng_session_trigger_list_build(state, name); | |
1669 | if (!trigger_list) { | |
1670 | goto error; | |
8abe313a JG |
1671 | } |
1672 | ||
1eee26c5 JG |
1673 | session = session_info_create(name, uid, gid, trigger_list, |
1674 | state->sessions_ht); | |
8abe313a JG |
1675 | if (!session) { |
1676 | ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)", | |
1677 | name, uid, gid); | |
b248644d | 1678 | lttng_session_trigger_list_destroy(trigger_list); |
ea9a44f0 | 1679 | goto error; |
8abe313a | 1680 | } |
ea9a44f0 | 1681 | trigger_list = NULL; |
577983cb JG |
1682 | |
1683 | cds_lfht_add(state->sessions_ht, hash_key_str(name, lttng_ht_seed), | |
9b63a4aa | 1684 | &session->sessions_ht_node); |
1eee26c5 | 1685 | end: |
8abe313a JG |
1686 | rcu_read_unlock(); |
1687 | return session; | |
ea9a44f0 JG |
1688 | error: |
1689 | rcu_read_unlock(); | |
1690 | session_info_put(session); | |
1691 | return NULL; | |
8abe313a JG |
1692 | } |
1693 | ||
ab0ee2ca JG |
1694 | static |
1695 | int handle_notification_thread_command_add_channel( | |
8abe313a JG |
1696 | struct notification_thread_state *state, |
1697 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1698 | const char *channel_name, enum lttng_domain_type channel_domain, | |
1699 | uint64_t channel_key_int, uint64_t channel_capacity, | |
1700 | enum lttng_error_code *cmd_result) | |
ab0ee2ca JG |
1701 | { |
1702 | struct cds_list_head trigger_list; | |
8abe313a JG |
1703 | struct channel_info *new_channel_info = NULL; |
1704 | struct channel_key channel_key = { | |
1705 | .key = channel_key_int, | |
1706 | .domain = channel_domain, | |
1707 | }; | |
ab0ee2ca JG |
1708 | struct lttng_channel_trigger_list *channel_trigger_list = NULL; |
1709 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
1710 | int trigger_count = 0; | |
1711 | struct cds_lfht_iter iter; | |
8abe313a | 1712 | struct session_info *session_info = NULL; |
ab0ee2ca JG |
1713 | |
1714 | DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64 " in %s domain", | |
8abe313a | 1715 | channel_name, session_name, channel_key_int, |
526200e4 | 1716 | lttng_domain_type_str(channel_domain)); |
ab0ee2ca JG |
1717 | |
1718 | CDS_INIT_LIST_HEAD(&trigger_list); | |
1719 | ||
8abe313a JG |
1720 | session_info = find_or_create_session_info(state, session_name, |
1721 | session_uid, session_gid); | |
1722 | if (!session_info) { | |
a9577b76 | 1723 | /* Allocation error or an internal error occurred. */ |
ab0ee2ca JG |
1724 | goto error; |
1725 | } | |
1726 | ||
8abe313a JG |
1727 | new_channel_info = channel_info_create(channel_name, &channel_key, |
1728 | channel_capacity, session_info); | |
1729 | if (!new_channel_info) { | |
1730 | goto error; | |
1731 | } | |
ab0ee2ca | 1732 | |
83b934ad | 1733 | rcu_read_lock(); |
ab0ee2ca JG |
1734 | /* Build a list of all triggers applying to the new channel. */ |
1735 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, | |
1736 | node) { | |
1737 | struct lttng_trigger_list_element *new_element; | |
1738 | ||
1739 | if (!trigger_applies_to_channel(trigger_ht_element->trigger, | |
8abe313a | 1740 | new_channel_info)) { |
ab0ee2ca JG |
1741 | continue; |
1742 | } | |
1743 | ||
1744 | new_element = zmalloc(sizeof(*new_element)); | |
1745 | if (!new_element) { | |
83b934ad | 1746 | rcu_read_unlock(); |
ab0ee2ca JG |
1747 | goto error; |
1748 | } | |
1749 | CDS_INIT_LIST_HEAD(&new_element->node); | |
1750 | new_element->trigger = trigger_ht_element->trigger; | |
1751 | cds_list_add(&new_element->node, &trigger_list); | |
1752 | trigger_count++; | |
1753 | } | |
83b934ad | 1754 | rcu_read_unlock(); |
ab0ee2ca JG |
1755 | |
1756 | DBG("[notification-thread] Found %i triggers that apply to newly added channel", | |
1757 | trigger_count); | |
1758 | channel_trigger_list = zmalloc(sizeof(*channel_trigger_list)); | |
1759 | if (!channel_trigger_list) { | |
1760 | goto error; | |
1761 | } | |
8abe313a | 1762 | channel_trigger_list->channel_key = new_channel_info->key; |
ab0ee2ca JG |
1763 | CDS_INIT_LIST_HEAD(&channel_trigger_list->list); |
1764 | cds_lfht_node_init(&channel_trigger_list->channel_triggers_ht_node); | |
1765 | cds_list_splice(&trigger_list, &channel_trigger_list->list); | |
1766 | ||
1767 | rcu_read_lock(); | |
1768 | /* Add channel to the channel_ht which owns the channel_infos. */ | |
1769 | cds_lfht_add(state->channels_ht, | |
8abe313a | 1770 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1771 | &new_channel_info->channels_ht_node); |
1772 | /* | |
1773 | * Add the list of triggers associated with this channel to the | |
1774 | * channel_triggers_ht. | |
1775 | */ | |
1776 | cds_lfht_add(state->channel_triggers_ht, | |
8abe313a | 1777 | hash_channel_key(&new_channel_info->key), |
ab0ee2ca JG |
1778 | &channel_trigger_list->channel_triggers_ht_node); |
1779 | rcu_read_unlock(); | |
1eee26c5 | 1780 | session_info_put(session_info); |
ab0ee2ca JG |
1781 | *cmd_result = LTTNG_OK; |
1782 | return 0; | |
1783 | error: | |
ab0ee2ca | 1784 | channel_info_destroy(new_channel_info); |
8abe313a | 1785 | session_info_put(session_info); |
ab0ee2ca JG |
1786 | return 1; |
1787 | } | |
1788 | ||
83b934ad MD |
1789 | static |
1790 | void free_channel_trigger_list_rcu(struct rcu_head *node) | |
1791 | { | |
1792 | free(caa_container_of(node, struct lttng_channel_trigger_list, | |
1793 | rcu_node)); | |
1794 | } | |
1795 | ||
1796 | static | |
1797 | void free_channel_state_sample_rcu(struct rcu_head *node) | |
1798 | { | |
1799 | free(caa_container_of(node, struct channel_state_sample, | |
1800 | rcu_node)); | |
1801 | } | |
1802 | ||
ab0ee2ca JG |
1803 | static |
1804 | int handle_notification_thread_command_remove_channel( | |
1805 | struct notification_thread_state *state, | |
1806 | uint64_t channel_key, enum lttng_domain_type domain, | |
1807 | enum lttng_error_code *cmd_result) | |
1808 | { | |
1809 | struct cds_lfht_node *node; | |
1810 | struct cds_lfht_iter iter; | |
1811 | struct lttng_channel_trigger_list *trigger_list; | |
1812 | struct lttng_trigger_list_element *trigger_list_element, *tmp; | |
1813 | struct channel_key key = { .key = channel_key, .domain = domain }; | |
1814 | struct channel_info *channel_info; | |
1815 | ||
1816 | DBG("[notification-thread] Removing channel key = %" PRIu64 " in %s domain", | |
526200e4 | 1817 | channel_key, lttng_domain_type_str(domain)); |
ab0ee2ca JG |
1818 | |
1819 | rcu_read_lock(); | |
1820 | ||
1821 | cds_lfht_lookup(state->channel_triggers_ht, | |
1822 | hash_channel_key(&key), | |
1823 | match_channel_trigger_list, | |
1824 | &key, | |
1825 | &iter); | |
1826 | node = cds_lfht_iter_get_node(&iter); | |
1827 | /* | |
1828 | * There is a severe internal error if we are being asked to remove a | |
1829 | * channel that doesn't exist. | |
1830 | */ | |
1831 | if (!node) { | |
1832 | ERR("[notification-thread] Channel being removed is unknown to the notification thread"); | |
1833 | goto end; | |
1834 | } | |
1835 | ||
1836 | /* Free the list of triggers associated with this channel. */ | |
1837 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, | |
1838 | channel_triggers_ht_node); | |
1839 | cds_list_for_each_entry_safe(trigger_list_element, tmp, | |
1840 | &trigger_list->list, node) { | |
1841 | cds_list_del(&trigger_list_element->node); | |
1842 | free(trigger_list_element); | |
1843 | } | |
1844 | cds_lfht_del(state->channel_triggers_ht, node); | |
83b934ad | 1845 | call_rcu(&trigger_list->rcu_node, free_channel_trigger_list_rcu); |
ab0ee2ca JG |
1846 | |
1847 | /* Free sampled channel state. */ | |
1848 | cds_lfht_lookup(state->channel_state_ht, | |
1849 | hash_channel_key(&key), | |
1850 | match_channel_state_sample, | |
1851 | &key, | |
1852 | &iter); | |
1853 | node = cds_lfht_iter_get_node(&iter); | |
1854 | /* | |
1855 | * This is expected to be NULL if the channel is destroyed before we | |
1856 | * received a sample. | |
1857 | */ | |
1858 | if (node) { | |
1859 | struct channel_state_sample *sample = caa_container_of(node, | |
1860 | struct channel_state_sample, | |
1861 | channel_state_ht_node); | |
1862 | ||
1863 | cds_lfht_del(state->channel_state_ht, node); | |
83b934ad | 1864 | call_rcu(&sample->rcu_node, free_channel_state_sample_rcu); |
ab0ee2ca JG |
1865 | } |
1866 | ||
1867 | /* Remove the channel from the channels_ht and free it. */ | |
1868 | cds_lfht_lookup(state->channels_ht, | |
1869 | hash_channel_key(&key), | |
1870 | match_channel_info, | |
1871 | &key, | |
1872 | &iter); | |
1873 | node = cds_lfht_iter_get_node(&iter); | |
1874 | assert(node); | |
1875 | channel_info = caa_container_of(node, struct channel_info, | |
1876 | channels_ht_node); | |
1877 | cds_lfht_del(state->channels_ht, node); | |
1878 | channel_info_destroy(channel_info); | |
1879 | end: | |
1880 | rcu_read_unlock(); | |
1881 | *cmd_result = LTTNG_OK; | |
1882 | return 0; | |
1883 | } | |
1884 | ||
0ca52944 | 1885 | static |
ed327204 | 1886 | int handle_notification_thread_command_session_rotation( |
0ca52944 | 1887 | struct notification_thread_state *state, |
ed327204 JG |
1888 | enum notification_thread_command_type cmd_type, |
1889 | const char *session_name, uid_t session_uid, gid_t session_gid, | |
1890 | uint64_t trace_archive_chunk_id, | |
1891 | struct lttng_trace_archive_location *location, | |
1892 | enum lttng_error_code *_cmd_result) | |
0ca52944 | 1893 | { |
ed327204 JG |
1894 | int ret = 0; |
1895 | enum lttng_error_code cmd_result = LTTNG_OK; | |
1896 | struct lttng_session_trigger_list *trigger_list; | |
1897 | struct lttng_trigger_list_element *trigger_list_element; | |
1898 | struct session_info *session_info; | |
f2b3ef9f | 1899 | const struct lttng_credentials session_creds = { |
ff588497 JR |
1900 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session_uid), |
1901 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session_gid), | |
f2b3ef9f | 1902 | }; |
0ca52944 | 1903 | |
ed327204 JG |
1904 | rcu_read_lock(); |
1905 | ||
1906 | session_info = find_or_create_session_info(state, session_name, | |
1907 | session_uid, session_gid); | |
1908 | if (!session_info) { | |
a9577b76 | 1909 | /* Allocation error or an internal error occurred. */ |
ed327204 JG |
1910 | ret = -1; |
1911 | cmd_result = LTTNG_ERR_NOMEM; | |
1912 | goto end; | |
1913 | } | |
1914 | ||
1915 | session_info->rotation.ongoing = | |
1916 | cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; | |
1917 | session_info->rotation.id = trace_archive_chunk_id; | |
1918 | trigger_list = get_session_trigger_list(state, session_name); | |
1919 | if (!trigger_list) { | |
1920 | DBG("[notification-thread] No triggers applying to session \"%s\" found", | |
1921 | session_name); | |
1922 | goto end; | |
1923 | } | |
1924 | ||
1925 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
1926 | node) { | |
1927 | const struct lttng_condition *condition; | |
f2b3ef9f | 1928 | struct lttng_trigger *trigger; |
ed327204 JG |
1929 | struct notification_client_list *client_list; |
1930 | struct lttng_evaluation *evaluation = NULL; | |
1931 | enum lttng_condition_type condition_type; | |
f2b3ef9f | 1932 | enum action_executor_status executor_status; |
ed327204 JG |
1933 | |
1934 | trigger = trigger_list_element->trigger; | |
1935 | condition = lttng_trigger_get_const_condition(trigger); | |
1936 | assert(condition); | |
1937 | condition_type = lttng_condition_get_type(condition); | |
1938 | ||
1939 | if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING && | |
1940 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { | |
1941 | continue; | |
1942 | } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED && | |
1943 | cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) { | |
1944 | continue; | |
1945 | } | |
1946 | ||
ed327204 | 1947 | client_list = get_client_list_from_condition(state, condition); |
ed327204 JG |
1948 | if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) { |
1949 | evaluation = lttng_evaluation_session_rotation_ongoing_create( | |
1950 | trace_archive_chunk_id); | |
1951 | } else { | |
1952 | evaluation = lttng_evaluation_session_rotation_completed_create( | |
1953 | trace_archive_chunk_id, location); | |
1954 | } | |
1955 | ||
1956 | if (!evaluation) { | |
1957 | /* Internal error */ | |
1958 | ret = -1; | |
1959 | cmd_result = LTTNG_ERR_UNK; | |
505b2d90 | 1960 | goto put_list; |
ed327204 JG |
1961 | } |
1962 | ||
f2b3ef9f JG |
1963 | /* |
1964 | * Ownership of `evaluation` transferred to the action executor | |
1965 | * no matter the result. | |
1966 | */ | |
72365501 JR |
1967 | executor_status = action_executor_enqueue_trigger( |
1968 | state->executor, trigger, evaluation, | |
1969 | &session_creds, client_list); | |
f2b3ef9f JG |
1970 | evaluation = NULL; |
1971 | switch (executor_status) { | |
1972 | case ACTION_EXECUTOR_STATUS_OK: | |
1973 | break; | |
1974 | case ACTION_EXECUTOR_STATUS_ERROR: | |
1975 | case ACTION_EXECUTOR_STATUS_INVALID: | |
1976 | /* | |
1977 | * TODO Add trigger identification (name/id) when | |
1978 | * it is added to the API. | |
1979 | */ | |
1980 | ERR("Fatal error occurred while enqueuing action associated with session rotation trigger"); | |
1981 | ret = -1; | |
1982 | goto put_list; | |
1983 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
1984 | /* | |
1985 | * TODO Add trigger identification (name/id) when | |
1986 | * it is added to the API. | |
1987 | * | |
1988 | * Not a fatal error. | |
1989 | */ | |
1990 | WARN("No space left when enqueuing action associated with session rotation trigger"); | |
1991 | ret = 0; | |
1992 | goto put_list; | |
1993 | default: | |
1994 | abort(); | |
1995 | } | |
1996 | ||
505b2d90 JG |
1997 | put_list: |
1998 | notification_client_list_put(client_list); | |
ed327204 | 1999 | if (caa_unlikely(ret)) { |
505b2d90 | 2000 | break; |
ed327204 JG |
2001 | } |
2002 | } | |
2003 | end: | |
2004 | session_info_put(session_info); | |
2005 | *_cmd_result = cmd_result; | |
2006 | rcu_read_unlock(); | |
2007 | return ret; | |
0ca52944 JG |
2008 | } |
2009 | ||
d02d7404 JR |
2010 | static |
2011 | int handle_notification_thread_command_add_tracer_event_source( | |
2012 | struct notification_thread_state *state, | |
2013 | int tracer_event_source_fd, | |
2014 | enum lttng_domain_type domain_type, | |
2015 | enum lttng_error_code *_cmd_result) | |
2016 | { | |
2017 | int ret = 0; | |
2018 | enum lttng_error_code cmd_result = LTTNG_OK; | |
2019 | struct notification_event_tracer_event_source_element *element = NULL; | |
2020 | ||
2021 | element = zmalloc(sizeof(*element)); | |
2022 | if (!element) { | |
2023 | cmd_result = LTTNG_ERR_NOMEM; | |
2024 | ret = -1; | |
2025 | goto end; | |
2026 | } | |
2027 | ||
d02d7404 JR |
2028 | element->fd = tracer_event_source_fd; |
2029 | element->domain = domain_type; | |
2030 | ||
2031 | cds_list_add(&element->node, &state->tracer_event_sources_list); | |
2032 | ||
2033 | DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'", | |
2034 | tracer_event_source_fd, | |
2035 | lttng_domain_type_str(domain_type)); | |
2036 | ||
2037 | /* Adding the read side pipe to the event poll. */ | |
2038 | ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR); | |
2039 | if (ret < 0) { | |
2040 | ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'", | |
2041 | tracer_event_source_fd, | |
2042 | lttng_domain_type_str(element->domain)); | |
2043 | cds_list_del(&element->node); | |
2044 | free(element); | |
2045 | goto end; | |
2046 | } | |
2047 | ||
2048 | element->is_fd_in_poll_set = true; | |
2049 | ||
2050 | end: | |
2051 | *_cmd_result = cmd_result; | |
2052 | return ret; | |
2053 | } | |
2054 | ||
8b524060 FD |
2055 | static |
2056 | int drain_event_notifier_notification_pipe( | |
2057 | struct notification_thread_state *state, | |
2058 | int pipe, enum lttng_domain_type domain) | |
2059 | { | |
2060 | struct lttng_poll_event events = {0}; | |
2061 | int ret; | |
2062 | ||
2063 | ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC); | |
2064 | if (ret < 0) { | |
2065 | ERR("[notification-thread] Error creating lttng_poll_event"); | |
2066 | goto end; | |
2067 | } | |
2068 | ||
2069 | ret = lttng_poll_add(&events, pipe, LPOLLIN); | |
2070 | if (ret < 0) { | |
2071 | ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d", | |
2072 | pipe); | |
2073 | goto end; | |
2074 | } | |
2075 | ||
2076 | while (true) { | |
2077 | /* | |
2078 | * Continue to consume notifications as long as there are new | |
2079 | * ones coming in. The tracer has been asked to stop producing | |
2080 | * them. | |
2081 | * | |
2082 | * LPOLLIN is explicitly checked since LPOLLHUP is implicitly | |
2083 | * monitored (on Linux, at least) and will be returned when | |
2084 | * the pipe is closed but empty. | |
2085 | */ | |
2086 | ret = lttng_poll_wait_interruptible(&events, 0); | |
a17b133b | 2087 | if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) { |
8b524060 FD |
2088 | /* No more notification to be read on this pipe. */ |
2089 | ret = 0; | |
2090 | goto end; | |
2091 | } else if (ret < 0) { | |
2092 | PERROR("Failed on lttng_poll_wait_interruptible() call"); | |
2093 | ret = -1; | |
2094 | goto end; | |
2095 | } | |
2096 | ||
2097 | ret = handle_one_event_notifier_notification(state, pipe, domain); | |
2098 | if (ret) { | |
2099 | ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d", | |
2100 | pipe); | |
2101 | } | |
2102 | } | |
2103 | end: | |
2104 | lttng_poll_clean(&events); | |
2105 | return ret; | |
2106 | } | |
2107 | ||
d02d7404 JR |
2108 | static |
2109 | int handle_notification_thread_command_remove_tracer_event_source( | |
2110 | struct notification_thread_state *state, | |
2111 | int tracer_event_source_fd, | |
2112 | enum lttng_error_code *_cmd_result) | |
2113 | { | |
2114 | int ret = 0; | |
2fb5e9b7 | 2115 | bool found = false; |
d02d7404 JR |
2116 | enum lttng_error_code cmd_result = LTTNG_OK; |
2117 | struct notification_event_tracer_event_source_element *source_element = NULL, *tmp; | |
2118 | ||
2119 | cds_list_for_each_entry_safe(source_element, tmp, | |
2120 | &state->tracer_event_sources_list, node) { | |
2121 | if (source_element->fd != tracer_event_source_fd) { | |
2122 | continue; | |
2123 | } | |
2124 | ||
2125 | DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", | |
2126 | tracer_event_source_fd, | |
2127 | lttng_domain_type_str(source_element->domain)); | |
2128 | cds_list_del(&source_element->node); | |
2fb5e9b7 | 2129 | found = true; |
d02d7404 JR |
2130 | break; |
2131 | } | |
2132 | ||
2fb5e9b7 JG |
2133 | if (!found) { |
2134 | /* | |
2135 | * This is temporarily allowed since the poll activity set is | |
2136 | * not properly cleaned-up for the moment. This is adressed in | |
2137 | * an upcoming fix. | |
2138 | */ | |
2139 | source_element = NULL; | |
2140 | goto end; | |
2141 | } | |
d02d7404 JR |
2142 | |
2143 | if (!source_element->is_fd_in_poll_set) { | |
2144 | /* Skip the poll set removal. */ | |
2145 | goto end; | |
2146 | } | |
2147 | ||
2148 | DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", | |
2149 | tracer_event_source_fd, | |
2150 | lttng_domain_type_str(source_element->domain)); | |
2151 | ||
2152 | /* Removing the fd from the event poll set. */ | |
2153 | ret = lttng_poll_del(&state->events, tracer_event_source_fd); | |
2154 | if (ret < 0) { | |
2155 | ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'", | |
2156 | tracer_event_source_fd, | |
2157 | lttng_domain_type_str(source_element->domain)); | |
2158 | cmd_result = LTTNG_ERR_FATAL; | |
2159 | goto end; | |
2160 | } | |
2161 | ||
173900f6 JG |
2162 | source_element->is_fd_in_poll_set = false; |
2163 | ||
8b524060 FD |
2164 | ret = drain_event_notifier_notification_pipe(state, tracer_event_source_fd, |
2165 | source_element->domain); | |
2166 | if (ret) { | |
2167 | ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s", | |
2168 | tracer_event_source_fd, | |
2169 | lttng_domain_type_str(source_element->domain)); | |
2170 | cmd_result = LTTNG_ERR_FATAL; | |
2171 | goto end; | |
2172 | } | |
2173 | ||
2174 | /* | |
2175 | * The drain_event_notifier_notification_pipe() call might have read | |
2176 | * data from an fd that we received in event in the latest _poll_wait() | |
2177 | * call. Make sure the thread call poll_wait() again to ensure we have | |
2178 | * a clean state. | |
2179 | */ | |
2180 | state->restart_poll = true; | |
2181 | ||
d02d7404 JR |
2182 | end: |
2183 | free(source_element); | |
2184 | *_cmd_result = cmd_result; | |
2185 | return ret; | |
2186 | } | |
2187 | ||
2188 | int handle_notification_thread_remove_tracer_event_source_no_result( | |
2189 | struct notification_thread_state *state, | |
2190 | int tracer_event_source_fd) | |
2191 | { | |
2192 | int ret; | |
2193 | enum lttng_error_code cmd_result; | |
2194 | ||
2195 | ret = handle_notification_thread_command_remove_tracer_event_source( | |
2196 | state, tracer_event_source_fd, &cmd_result); | |
2197 | (void) cmd_result; | |
2198 | return ret; | |
2199 | } | |
2200 | ||
fbc9f37d JR |
2201 | static int handle_notification_thread_command_list_triggers( |
2202 | struct notification_thread_handle *handle, | |
2203 | struct notification_thread_state *state, | |
2204 | uid_t client_uid, | |
2205 | struct lttng_triggers **triggers, | |
2206 | enum lttng_error_code *_cmd_result) | |
2207 | { | |
2208 | int ret = 0; | |
2209 | enum lttng_error_code cmd_result = LTTNG_OK; | |
2210 | struct cds_lfht_iter iter; | |
2211 | struct lttng_trigger_ht_element *trigger_ht_element; | |
2212 | struct lttng_triggers *local_triggers = NULL; | |
2213 | const struct lttng_credentials *creds; | |
2214 | ||
2215 | rcu_read_lock(); | |
2216 | ||
2217 | local_triggers = lttng_triggers_create(); | |
2218 | if (!local_triggers) { | |
2219 | /* Not a fatal error. */ | |
2220 | cmd_result = LTTNG_ERR_NOMEM; | |
2221 | goto end; | |
2222 | } | |
2223 | ||
2224 | cds_lfht_for_each_entry(state->triggers_ht, &iter, | |
2225 | trigger_ht_element, node) { | |
2226 | /* | |
2227 | * Only return the triggers to which the client has access. | |
2228 | * The root user has visibility over all triggers. | |
2229 | */ | |
2230 | creds = lttng_trigger_get_credentials(trigger_ht_element->trigger); | |
2231 | if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) { | |
2232 | continue; | |
2233 | } | |
2234 | ||
2235 | ret = lttng_triggers_add(local_triggers, | |
2236 | trigger_ht_element->trigger); | |
2237 | if (ret < 0) { | |
2238 | /* Not a fatal error. */ | |
2239 | ret = 0; | |
2240 | cmd_result = LTTNG_ERR_NOMEM; | |
2241 | goto end; | |
2242 | } | |
2243 | } | |
2244 | ||
2245 | /* Transferring ownership to the caller. */ | |
2246 | *triggers = local_triggers; | |
2247 | local_triggers = NULL; | |
2248 | ||
2249 | end: | |
2250 | rcu_read_unlock(); | |
2251 | lttng_triggers_destroy(local_triggers); | |
2252 | *_cmd_result = cmd_result; | |
2253 | return ret; | |
2254 | } | |
2255 | ||
8790759c FD |
2256 | static inline void get_trigger_info_for_log(const struct lttng_trigger *trigger, |
2257 | const char **trigger_name, | |
2258 | uid_t *trigger_owner_uid) | |
2259 | { | |
2260 | enum lttng_trigger_status trigger_status; | |
2261 | ||
2262 | trigger_status = lttng_trigger_get_name(trigger, trigger_name); | |
2263 | switch (trigger_status) { | |
2264 | case LTTNG_TRIGGER_STATUS_OK: | |
2265 | break; | |
2266 | case LTTNG_TRIGGER_STATUS_UNSET: | |
0efb2ad7 | 2267 | *trigger_name = "(anonymous)"; |
8790759c FD |
2268 | break; |
2269 | default: | |
2270 | abort(); | |
2271 | } | |
2272 | ||
2273 | trigger_status = lttng_trigger_get_owner_uid(trigger, | |
2274 | trigger_owner_uid); | |
2275 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); | |
2276 | } | |
2277 | ||
2278 | static int handle_notification_thread_command_get_trigger( | |
2279 | struct notification_thread_state *state, | |
2280 | const struct lttng_trigger *trigger, | |
2281 | struct lttng_trigger **registered_trigger, | |
2282 | enum lttng_error_code *_cmd_result) | |
2283 | { | |
2284 | int ret = -1; | |
2285 | struct cds_lfht_iter iter; | |
2286 | struct lttng_trigger_ht_element *trigger_ht_element; | |
2287 | enum lttng_error_code cmd_result = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
2288 | const char *trigger_name; | |
2289 | uid_t trigger_owner_uid; | |
2290 | ||
2291 | rcu_read_lock(); | |
2292 | ||
2293 | cds_lfht_for_each_entry( | |
2294 | state->triggers_ht, &iter, trigger_ht_element, node) { | |
2295 | if (lttng_trigger_is_equal( | |
2296 | trigger, trigger_ht_element->trigger)) { | |
2297 | /* Take one reference on the return trigger. */ | |
2298 | *registered_trigger = trigger_ht_element->trigger; | |
2299 | lttng_trigger_get(*registered_trigger); | |
2300 | ret = 0; | |
2301 | cmd_result = LTTNG_OK; | |
2302 | goto end; | |
2303 | } | |
2304 | } | |
2305 | ||
2306 | /* Not a fatal error if the trigger is not found. */ | |
2307 | get_trigger_info_for_log(trigger, &trigger_name, &trigger_owner_uid); | |
87661585 | 2308 | DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d", |
8790759c FD |
2309 | trigger_name, (int) trigger_owner_uid); |
2310 | ||
2311 | ret = 0; | |
2312 | ||
2313 | end: | |
2314 | rcu_read_unlock(); | |
2315 | *_cmd_result = cmd_result; | |
2316 | return ret; | |
2317 | } | |
2318 | ||
1da26331 | 2319 | static |
959e3c66 | 2320 | bool condition_is_supported(struct lttng_condition *condition) |
1da26331 | 2321 | { |
959e3c66 | 2322 | bool is_supported; |
1da26331 JG |
2323 | |
2324 | switch (lttng_condition_get_type(condition)) { | |
2325 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
2326 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
2327 | { | |
959e3c66 | 2328 | int ret; |
1da26331 JG |
2329 | enum lttng_domain_type domain; |
2330 | ||
2331 | ret = lttng_condition_buffer_usage_get_domain_type(condition, | |
2332 | &domain); | |
959e3c66 | 2333 | assert(ret == 0); |
1da26331 JG |
2334 | |
2335 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
959e3c66 | 2336 | is_supported = true; |
1da26331 JG |
2337 | goto end; |
2338 | } | |
2339 | ||
2340 | /* | |
2341 | * Older kernel tracers don't expose the API to monitor their | |
2342 | * buffers. Therefore, we reject triggers that require that | |
2343 | * mechanism to be available to be evaluated. | |
959e3c66 JR |
2344 | * |
2345 | * Assume unsupported on error. | |
1da26331 | 2346 | */ |
959e3c66 JR |
2347 | is_supported = kernel_supports_ring_buffer_snapshot_sample_positions() == 1; |
2348 | break; | |
2349 | } | |
8dbb86b8 | 2350 | case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES: |
959e3c66 JR |
2351 | { |
2352 | const struct lttng_event_rule *event_rule; | |
2353 | enum lttng_domain_type domain; | |
2354 | const enum lttng_condition_status status = | |
8dbb86b8 | 2355 | lttng_condition_event_rule_matches_get_rule( |
959e3c66 JR |
2356 | condition, &event_rule); |
2357 | ||
2358 | assert(status == LTTNG_CONDITION_STATUS_OK); | |
2359 | ||
2360 | domain = lttng_event_rule_get_domain_type(event_rule); | |
2361 | if (domain != LTTNG_DOMAIN_KERNEL) { | |
2362 | is_supported = true; | |
2363 | goto end; | |
2364 | } | |
2365 | ||
2366 | /* | |
2367 | * Older kernel tracers can't emit notification. Therefore, we | |
2368 | * reject triggers that require that mechanism to be available | |
2369 | * to be evaluated. | |
2370 | * | |
2371 | * Assume unsupported on error. | |
2372 | */ | |
2373 | is_supported = kernel_supports_event_notifiers() == 1; | |
1da26331 JG |
2374 | break; |
2375 | } | |
2376 | default: | |
959e3c66 | 2377 | is_supported = true; |
1da26331 JG |
2378 | } |
2379 | end: | |
959e3c66 | 2380 | return is_supported; |
1da26331 JG |
2381 | } |
2382 | ||
51eab943 JG |
2383 | /* Must be called with RCU read lock held. */ |
2384 | static | |
f2b3ef9f | 2385 | int bind_trigger_to_matching_session(struct lttng_trigger *trigger, |
51eab943 JG |
2386 | struct notification_thread_state *state) |
2387 | { | |
2388 | int ret = 0; | |
51eab943 JG |
2389 | const struct lttng_condition *condition; |
2390 | const char *session_name; | |
2391 | struct lttng_session_trigger_list *trigger_list; | |
2392 | ||
2393 | condition = lttng_trigger_get_const_condition(trigger); | |
2394 | switch (lttng_condition_get_type(condition)) { | |
2395 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: | |
2396 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
2397 | { | |
2398 | enum lttng_condition_status status; | |
2399 | ||
2400 | status = lttng_condition_session_rotation_get_session_name( | |
2401 | condition, &session_name); | |
2402 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
2403 | ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name"); | |
2404 | ret = -1; | |
2405 | goto end; | |
2406 | } | |
2407 | break; | |
2408 | } | |
2409 | default: | |
2410 | ret = -1; | |
2411 | goto end; | |
2412 | } | |
2413 | ||
ed327204 JG |
2414 | trigger_list = get_session_trigger_list(state, session_name); |
2415 | if (!trigger_list) { | |
51eab943 JG |
2416 | DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system", |
2417 | session_name); | |
2418 | goto end; | |
51eab943 | 2419 | |
ed327204 | 2420 | } |
51eab943 JG |
2421 | |
2422 | DBG("[notification-thread] Newly registered trigger bound to session \"%s\"", | |
2423 | session_name); | |
2424 | ret = lttng_session_trigger_list_add(trigger_list, trigger); | |
2425 | end: | |
2426 | return ret; | |
2427 | } | |
2428 | ||
2429 | /* Must be called with RCU read lock held. */ | |
2430 | static | |
f2b3ef9f | 2431 | int bind_trigger_to_matching_channels(struct lttng_trigger *trigger, |
51eab943 JG |
2432 | struct notification_thread_state *state) |
2433 | { | |
2434 | int ret = 0; | |
2435 | struct cds_lfht_node *node; | |
2436 | struct cds_lfht_iter iter; | |
2437 | struct channel_info *channel; | |
2438 | ||
2439 | cds_lfht_for_each_entry(state->channels_ht, &iter, channel, | |
2440 | channels_ht_node) { | |
2441 | struct lttng_trigger_list_element *trigger_list_element; | |
2442 | struct lttng_channel_trigger_list *trigger_list; | |
a5d64ae7 | 2443 | struct cds_lfht_iter lookup_iter; |
51eab943 JG |
2444 | |
2445 | if (!trigger_applies_to_channel(trigger, channel)) { | |
2446 | continue; | |
2447 | } | |
2448 | ||
2449 | cds_lfht_lookup(state->channel_triggers_ht, | |
2450 | hash_channel_key(&channel->key), | |
2451 | match_channel_trigger_list, | |
2452 | &channel->key, | |
a5d64ae7 MD |
2453 | &lookup_iter); |
2454 | node = cds_lfht_iter_get_node(&lookup_iter); | |
51eab943 JG |
2455 | assert(node); |
2456 | trigger_list = caa_container_of(node, | |
2457 | struct lttng_channel_trigger_list, | |
2458 | channel_triggers_ht_node); | |
2459 | ||
2460 | trigger_list_element = zmalloc(sizeof(*trigger_list_element)); | |
2461 | if (!trigger_list_element) { | |
2462 | ret = -1; | |
2463 | goto end; | |
2464 | } | |
2465 | CDS_INIT_LIST_HEAD(&trigger_list_element->node); | |
2466 | trigger_list_element->trigger = trigger; | |
2467 | cds_list_add(&trigger_list_element->node, &trigger_list->list); | |
2468 | DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"", | |
2469 | channel->name); | |
2470 | } | |
2471 | end: | |
2472 | return ret; | |
2473 | } | |
2474 | ||
f2b3ef9f JG |
2475 | static |
2476 | bool is_trigger_action_notify(const struct lttng_trigger *trigger) | |
2477 | { | |
2478 | bool is_notify = false; | |
2479 | unsigned int i, count; | |
2480 | enum lttng_action_status action_status; | |
2481 | const struct lttng_action *action = | |
2482 | lttng_trigger_get_const_action(trigger); | |
2483 | enum lttng_action_type action_type; | |
2484 | ||
2485 | assert(action); | |
17182cfd | 2486 | action_type = lttng_action_get_type(action); |
f2b3ef9f JG |
2487 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
2488 | is_notify = true; | |
2489 | goto end; | |
2490 | } else if (action_type != LTTNG_ACTION_TYPE_GROUP) { | |
2491 | goto end; | |
2492 | } | |
2493 | ||
702f26c8 | 2494 | action_status = lttng_action_list_get_count(action, &count); |
f2b3ef9f JG |
2495 | assert(action_status == LTTNG_ACTION_STATUS_OK); |
2496 | ||
2497 | for (i = 0; i < count; i++) { | |
2498 | const struct lttng_action *inner_action = | |
702f26c8 | 2499 | lttng_action_list_get_at_index( |
f2b3ef9f JG |
2500 | action, i); |
2501 | ||
17182cfd | 2502 | action_type = lttng_action_get_type(inner_action); |
f2b3ef9f JG |
2503 | if (action_type == LTTNG_ACTION_TYPE_NOTIFY) { |
2504 | is_notify = true; | |
2505 | goto end; | |
2506 | } | |
2507 | } | |
2508 | ||
2509 | end: | |
2510 | return is_notify; | |
2511 | } | |
2512 | ||
242388e4 JR |
2513 | static bool trigger_name_taken(struct notification_thread_state *state, |
2514 | const struct lttng_trigger *trigger) | |
2515 | { | |
2516 | struct cds_lfht_iter iter; | |
2517 | ||
2518 | /* | |
2519 | * No duplicata is allowed in the triggers_by_name_uid_ht. | |
2520 | * The match is done against the trigger name and uid. | |
2521 | */ | |
2522 | cds_lfht_lookup(state->triggers_by_name_uid_ht, | |
2523 | hash_trigger_by_name_uid(trigger), | |
2524 | match_trigger_by_name_uid, | |
2525 | trigger, | |
2526 | &iter); | |
2527 | return !!cds_lfht_iter_get_node(&iter); | |
2528 | } | |
2529 | ||
2530 | static | |
2531 | enum lttng_error_code generate_trigger_name( | |
2532 | struct notification_thread_state *state, | |
2533 | struct lttng_trigger *trigger, const char **name) | |
2534 | { | |
2535 | enum lttng_error_code ret_code = LTTNG_OK; | |
2536 | bool taken = false; | |
2537 | enum lttng_trigger_status status; | |
2538 | ||
2539 | do { | |
2540 | const int ret = lttng_trigger_generate_name(trigger, | |
2541 | state->trigger_id.name_offset++); | |
2542 | if (ret) { | |
2543 | /* The only reason this can fail right now. */ | |
2544 | ret_code = LTTNG_ERR_NOMEM; | |
2545 | break; | |
2546 | } | |
2547 | ||
2548 | status = lttng_trigger_get_name(trigger, name); | |
2549 | assert(status == LTTNG_TRIGGER_STATUS_OK); | |
2550 | ||
2551 | taken = trigger_name_taken(state, trigger); | |
2552 | } while (taken || state->trigger_id.name_offset == UINT64_MAX); | |
2553 | ||
2554 | return ret_code; | |
2555 | } | |
2556 | ||
2758e38b FD |
2557 | static inline |
2558 | void notif_thread_state_remove_trigger_ht_elem( | |
2559 | struct notification_thread_state *state, | |
2560 | struct lttng_trigger_ht_element *trigger_ht_element) | |
2561 | { | |
2562 | assert(state); | |
2563 | assert(trigger_ht_element); | |
2564 | ||
2565 | cds_lfht_del(state->triggers_ht, &trigger_ht_element->node); | |
2566 | cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid); | |
2567 | } | |
2568 | ||
6487ad53 FD |
2569 | static |
2570 | enum lttng_error_code setup_tracer_notifier( | |
2571 | struct notification_thread_state *state, | |
2572 | struct lttng_trigger *trigger) | |
2573 | { | |
2574 | enum lttng_error_code ret; | |
2575 | enum event_notifier_error_accounting_status error_accounting_status; | |
2576 | struct cds_lfht_node *node; | |
2577 | uint64_t error_counter_index = 0; | |
2578 | struct lttng_condition *condition = lttng_trigger_get_condition(trigger); | |
2579 | struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL; | |
2580 | ||
2581 | trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element)); | |
2582 | if (!trigger_tokens_ht_element) { | |
2583 | ret = LTTNG_ERR_NOMEM; | |
2584 | goto end; | |
2585 | } | |
2586 | ||
2587 | /* Add trigger token to the trigger_tokens_ht. */ | |
2588 | cds_lfht_node_init(&trigger_tokens_ht_element->node); | |
2589 | trigger_tokens_ht_element->token = LTTNG_OPTIONAL_GET(trigger->tracer_token); | |
2590 | trigger_tokens_ht_element->trigger = trigger; | |
2591 | ||
2592 | node = cds_lfht_add_unique(state->trigger_tokens_ht, | |
2593 | hash_key_u64(&trigger_tokens_ht_element->token, lttng_ht_seed), | |
2594 | match_trigger_token, | |
2595 | &trigger_tokens_ht_element->token, | |
2596 | &trigger_tokens_ht_element->node); | |
2597 | if (node != &trigger_tokens_ht_element->node) { | |
2598 | ret = LTTNG_ERR_TRIGGER_EXISTS; | |
2599 | goto error_free_ht_element; | |
2600 | } | |
2601 | ||
2602 | error_accounting_status = event_notifier_error_accounting_register_event_notifier( | |
2603 | trigger, &error_counter_index); | |
2604 | if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
2605 | if (error_accounting_status == EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE) { | |
2606 | DBG("Trigger group error accounting counter full."); | |
2607 | ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL; | |
2608 | } else { | |
2609 | ERR("Error registering trigger for error accounting"); | |
2610 | ret = LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION; | |
2611 | } | |
2612 | ||
2613 | goto error_remove_ht_element; | |
2614 | } | |
2615 | ||
8dbb86b8 | 2616 | lttng_condition_event_rule_matches_set_error_counter_index( |
6487ad53 FD |
2617 | condition, error_counter_index); |
2618 | ||
2619 | ret = LTTNG_OK; | |
2620 | goto end; | |
2621 | ||
2622 | error_remove_ht_element: | |
2623 | cds_lfht_del(state->trigger_tokens_ht, &trigger_tokens_ht_element->node); | |
2624 | error_free_ht_element: | |
2625 | free(trigger_tokens_ht_element); | |
2626 | end: | |
2627 | return ret; | |
2628 | } | |
2629 | ||
ab0ee2ca | 2630 | /* |
f2b3ef9f | 2631 | * FIXME A client's credentials are not checked when registering a trigger. |
ab0ee2ca | 2632 | * |
1da26331 | 2633 | * The effects of this are benign since: |
ab0ee2ca | 2634 | * - The client will succeed in registering the trigger, as it is valid, |
51eab943 | 2635 | * - The trigger will, internally, be bound to the channel/session, |
ab0ee2ca JG |
2636 | * - The notifications will not be sent since the client's credentials |
2637 | * are checked against the channel at that moment. | |
1da26331 JG |
2638 | * |
2639 | * If this function returns a non-zero value, it means something is | |
50ca7858 | 2640 | * fundamentally broken and the whole subsystem/thread will be torn down. |
1da26331 JG |
2641 | * |
2642 | * If a non-fatal error occurs, just set the cmd_result to the appropriate | |
2643 | * error code. | |
ab0ee2ca JG |
2644 | */ |
2645 | static | |
2646 | int handle_notification_thread_command_register_trigger( | |
8abe313a JG |
2647 | struct notification_thread_state *state, |
2648 | struct lttng_trigger *trigger, | |
0efb2ad7 | 2649 | bool is_trigger_anonymous, |
8abe313a | 2650 | enum lttng_error_code *cmd_result) |
ab0ee2ca JG |
2651 | { |
2652 | int ret = 0; | |
2653 | struct lttng_condition *condition; | |
ab0ee2ca JG |
2654 | struct notification_client_list *client_list = NULL; |
2655 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; | |
ab0ee2ca | 2656 | struct cds_lfht_node *node; |
242388e4 | 2657 | const char* trigger_name; |
ab0ee2ca | 2658 | bool free_trigger = true; |
f2b3ef9f JG |
2659 | struct lttng_evaluation *evaluation = NULL; |
2660 | struct lttng_credentials object_creds; | |
ff588497 JR |
2661 | uid_t object_uid; |
2662 | gid_t object_gid; | |
f2b3ef9f | 2663 | enum action_executor_status executor_status; |
e6887944 JR |
2664 | const uint64_t trigger_tracer_token = |
2665 | state->trigger_id.next_tracer_token++; | |
ab0ee2ca JG |
2666 | |
2667 | rcu_read_lock(); | |
2668 | ||
e6887944 JR |
2669 | /* Set the trigger's tracer token. */ |
2670 | lttng_trigger_set_tracer_token(trigger, trigger_tracer_token); | |
2671 | ||
0efb2ad7 JG |
2672 | if (!is_trigger_anonymous) { |
2673 | if (lttng_trigger_get_name(trigger, &trigger_name) == | |
2674 | LTTNG_TRIGGER_STATUS_UNSET) { | |
2675 | const enum lttng_error_code ret_code = | |
2676 | generate_trigger_name(state, trigger, | |
2677 | &trigger_name); | |
242388e4 | 2678 | |
0efb2ad7 JG |
2679 | if (ret_code != LTTNG_OK) { |
2680 | /* Fatal error. */ | |
2681 | ret = -1; | |
2682 | *cmd_result = ret_code; | |
2683 | goto error; | |
2684 | } | |
2685 | } else if (trigger_name_taken(state, trigger)) { | |
2686 | /* Not a fatal error. */ | |
2687 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2688 | ret = 0; | |
242388e4 JR |
2689 | goto error; |
2690 | } | |
0efb2ad7 JG |
2691 | } else { |
2692 | trigger_name = "(anonymous)"; | |
242388e4 JR |
2693 | } |
2694 | ||
ab0ee2ca | 2695 | condition = lttng_trigger_get_condition(trigger); |
1da26331 JG |
2696 | assert(condition); |
2697 | ||
959e3c66 JR |
2698 | /* Some conditions require tracers to implement a minimal ABI version. */ |
2699 | if (!condition_is_supported(condition)) { | |
1da26331 JG |
2700 | *cmd_result = LTTNG_ERR_NOT_SUPPORTED; |
2701 | goto error; | |
1da26331 JG |
2702 | } |
2703 | ||
ab0ee2ca JG |
2704 | trigger_ht_element = zmalloc(sizeof(*trigger_ht_element)); |
2705 | if (!trigger_ht_element) { | |
2706 | ret = -1; | |
2707 | goto error; | |
2708 | } | |
2709 | ||
2710 | /* Add trigger to the trigger_ht. */ | |
2711 | cds_lfht_node_init(&trigger_ht_element->node); | |
242388e4 | 2712 | cds_lfht_node_init(&trigger_ht_element->node_by_name_uid); |
ab0ee2ca JG |
2713 | trigger_ht_element->trigger = trigger; |
2714 | ||
2715 | node = cds_lfht_add_unique(state->triggers_ht, | |
2716 | lttng_condition_hash(condition), | |
242388e4 JR |
2717 | match_trigger, |
2718 | trigger, | |
ab0ee2ca JG |
2719 | &trigger_ht_element->node); |
2720 | if (node != &trigger_ht_element->node) { | |
2721 | /* Not a fatal error, simply report it to the client. */ | |
2722 | *cmd_result = LTTNG_ERR_TRIGGER_EXISTS; | |
2723 | goto error_free_ht_element; | |
2724 | } | |
2725 | ||
242388e4 JR |
2726 | node = cds_lfht_add_unique(state->triggers_by_name_uid_ht, |
2727 | hash_trigger_by_name_uid(trigger), | |
2728 | match_trigger_by_name_uid, | |
2729 | trigger, | |
2730 | &trigger_ht_element->node_by_name_uid); | |
2731 | if (node != &trigger_ht_element->node_by_name_uid) { | |
75e540c6 JG |
2732 | /* Internal error: add to triggers_ht should have failed. */ |
2733 | ret = -1; | |
242388e4 JR |
2734 | goto error_free_ht_element; |
2735 | } | |
2736 | ||
9c374932 JR |
2737 | /* From this point consider the trigger registered. */ |
2738 | lttng_trigger_set_as_registered(trigger); | |
2739 | ||
6487ad53 FD |
2740 | /* |
2741 | * Some triggers might need a tracer notifier depending on its | |
2742 | * condition and actions. | |
2743 | */ | |
2744 | if (lttng_trigger_needs_tracer_notifier(trigger)) { | |
2745 | enum lttng_error_code error_code; | |
e7c93cf9 | 2746 | |
6487ad53 FD |
2747 | error_code = setup_tracer_notifier(state, trigger); |
2748 | if (error_code != LTTNG_OK) { | |
2758e38b FD |
2749 | notif_thread_state_remove_trigger_ht_elem(state, |
2750 | trigger_ht_element); | |
6487ad53 FD |
2751 | if (error_code == LTTNG_ERR_NOMEM) { |
2752 | ret = -1; | |
2753 | } else { | |
2754 | *cmd_result = error_code; | |
2755 | ret = 0; | |
90aa04a1 FD |
2756 | } |
2757 | ||
6487ad53 | 2758 | goto error_free_ht_element; |
90aa04a1 | 2759 | } |
e7c93cf9 JR |
2760 | } |
2761 | ||
ab0ee2ca JG |
2762 | /* |
2763 | * The rest only applies to triggers that have a "notify" action. | |
2764 | * It is not skipped as this is the only action type currently | |
2765 | * supported. | |
2766 | */ | |
f2b3ef9f | 2767 | if (is_trigger_action_notify(trigger)) { |
091fa780 FD |
2768 | /* |
2769 | * Find or create the client list of this condition. It may | |
2770 | * already be present if another trigger is already registered | |
2771 | * with the same condition. | |
2772 | */ | |
2773 | client_list = get_client_list_from_condition(state, condition); | |
f2b3ef9f | 2774 | if (!client_list) { |
091fa780 FD |
2775 | /* |
2776 | * No client list for this condition yet. We create new | |
2777 | * one and build it up. | |
2778 | */ | |
2779 | client_list = notification_client_list_create(state, condition); | |
2780 | if (!client_list) { | |
2781 | ERR("Error creating notification client list for trigger %s", trigger->name); | |
2782 | goto error_free_ht_element; | |
f2b3ef9f | 2783 | } |
ab0ee2ca | 2784 | } |
f2b3ef9f | 2785 | |
091fa780 FD |
2786 | CDS_INIT_LIST_HEAD(&trigger_ht_element->client_list_trigger_node); |
2787 | ||
2788 | pthread_mutex_lock(&client_list->lock); | |
2789 | cds_list_add(&trigger_ht_element->client_list_trigger_node, &client_list->triggers_list); | |
2790 | pthread_mutex_unlock(&client_list->lock); | |
ab0ee2ca JG |
2791 | } |
2792 | ||
091fa780 FD |
2793 | /* |
2794 | * Ownership of the trigger and of its wrapper was transfered to | |
2795 | * the triggers_ht. Same for token ht element if necessary. | |
2796 | */ | |
2797 | trigger_ht_element = NULL; | |
2798 | free_trigger = false; | |
2799 | ||
f82f93a1 | 2800 | switch (get_condition_binding_object(condition)) { |
51eab943 JG |
2801 | case LTTNG_OBJECT_TYPE_SESSION: |
2802 | /* Add the trigger to the list if it matches a known session. */ | |
2803 | ret = bind_trigger_to_matching_session(trigger, state); | |
2804 | if (ret) { | |
091fa780 | 2805 | goto error_free_ht_element; |
ab0ee2ca | 2806 | } |
f82f93a1 | 2807 | break; |
51eab943 JG |
2808 | case LTTNG_OBJECT_TYPE_CHANNEL: |
2809 | /* | |
2810 | * Add the trigger to list of triggers bound to the channels | |
2811 | * currently known. | |
2812 | */ | |
2813 | ret = bind_trigger_to_matching_channels(trigger, state); | |
2814 | if (ret) { | |
091fa780 | 2815 | goto error_free_ht_element; |
ab0ee2ca | 2816 | } |
51eab943 JG |
2817 | break; |
2818 | case LTTNG_OBJECT_TYPE_NONE: | |
2819 | break; | |
2820 | default: | |
f2b3ef9f | 2821 | ERR("Unknown object type on which to bind a newly registered trigger was encountered"); |
51eab943 | 2822 | ret = -1; |
091fa780 | 2823 | goto error_free_ht_element; |
ab0ee2ca JG |
2824 | } |
2825 | ||
2ae99f0b | 2826 | /* |
f2b3ef9f JG |
2827 | * The new trigger's condition must be evaluated against the current |
2828 | * state. | |
2829 | * | |
2830 | * In the case of `notify` action, nothing preventing clients from | |
2831 | * subscribing to a condition before the corresponding trigger is | |
2832 | * registered, we have to evaluate this new condition right away. | |
2ae99f0b JG |
2833 | * |
2834 | * At some point, we were waiting for the next "evaluation" (e.g. on | |
2835 | * reception of a channel sample) to evaluate this new condition, but | |
2836 | * that was broken. | |
2837 | * | |
2838 | * The reason it was broken is that waiting for the next sample | |
2839 | * does not allow us to properly handle transitions for edge-triggered | |
2840 | * conditions. | |
2841 | * | |
2842 | * Consider this example: when we handle a new channel sample, we | |
2843 | * evaluate each conditions twice: once with the previous state, and | |
2844 | * again with the newest state. We then use those two results to | |
2845 | * determine whether a state change happened: a condition was false and | |
2846 | * became true. If a state change happened, we have to notify clients. | |
2847 | * | |
2848 | * Now, if a client subscribes to a given notification and registers | |
2849 | * a trigger *after* that subscription, we have to make sure the | |
2850 | * condition is evaluated at this point while considering only the | |
2851 | * current state. Otherwise, the next evaluation cycle may only see | |
2852 | * that the evaluations remain the same (true for samples n-1 and n) and | |
2853 | * the client will never know that the condition has been met. | |
2854 | */ | |
f2b3ef9f JG |
2855 | switch (get_condition_binding_object(condition)) { |
2856 | case LTTNG_OBJECT_TYPE_SESSION: | |
2857 | ret = evaluate_session_condition_for_client(condition, state, | |
ff588497 JR |
2858 | &evaluation, &object_uid, |
2859 | &object_gid); | |
bc8daafb JG |
2860 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
2861 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); | |
b42ada90 | 2862 | break; |
f2b3ef9f JG |
2863 | case LTTNG_OBJECT_TYPE_CHANNEL: |
2864 | ret = evaluate_channel_condition_for_client(condition, state, | |
ff588497 JR |
2865 | &evaluation, &object_uid, |
2866 | &object_gid); | |
bc8daafb JG |
2867 | LTTNG_OPTIONAL_SET(&object_creds.uid, object_uid); |
2868 | LTTNG_OPTIONAL_SET(&object_creds.gid, object_gid); | |
f2b3ef9f JG |
2869 | break; |
2870 | case LTTNG_OBJECT_TYPE_NONE: | |
2871 | ret = 0; | |
242388e4 | 2872 | break; |
f2b3ef9f JG |
2873 | case LTTNG_OBJECT_TYPE_UNKNOWN: |
2874 | default: | |
2875 | ret = -1; | |
242388e4 | 2876 | break; |
f2b3ef9f JG |
2877 | } |
2878 | ||
2879 | if (ret) { | |
2880 | /* Fatal error. */ | |
091fa780 | 2881 | goto error_free_ht_element; |
f2b3ef9f JG |
2882 | } |
2883 | ||
2884 | DBG("Newly registered trigger's condition evaluated to %s", | |
2885 | evaluation ? "true" : "false"); | |
2886 | if (!evaluation) { | |
2887 | /* Evaluation yielded nothing. Normal exit. */ | |
2888 | ret = 0; | |
091fa780 | 2889 | goto success; |
2ae99f0b JG |
2890 | } |
2891 | ||
2892 | /* | |
f2b3ef9f JG |
2893 | * Ownership of `evaluation` transferred to the action executor |
2894 | * no matter the result. | |
2ae99f0b | 2895 | */ |
72365501 JR |
2896 | executor_status = action_executor_enqueue_trigger(state->executor, |
2897 | trigger, evaluation, &object_creds, client_list); | |
f2b3ef9f JG |
2898 | evaluation = NULL; |
2899 | switch (executor_status) { | |
2900 | case ACTION_EXECUTOR_STATUS_OK: | |
2901 | break; | |
2902 | case ACTION_EXECUTOR_STATUS_ERROR: | |
2903 | case ACTION_EXECUTOR_STATUS_INVALID: | |
2904 | /* | |
2905 | * TODO Add trigger identification (name/id) when | |
2906 | * it is added to the API. | |
2907 | */ | |
2908 | ERR("Fatal error occurred while enqueuing action associated to newly registered trigger"); | |
2909 | ret = -1; | |
091fa780 | 2910 | goto error_free_ht_element; |
f2b3ef9f JG |
2911 | case ACTION_EXECUTOR_STATUS_OVERFLOW: |
2912 | /* | |
2913 | * TODO Add trigger identification (name/id) when | |
2914 | * it is added to the API. | |
2915 | * | |
2916 | * Not a fatal error. | |
2917 | */ | |
2918 | WARN("No space left when enqueuing action associated to newly registered trigger"); | |
2919 | ret = 0; | |
091fa780 | 2920 | goto success; |
f2b3ef9f JG |
2921 | default: |
2922 | abort(); | |
2923 | } | |
2ae99f0b | 2924 | |
091fa780 | 2925 | success: |
ab0ee2ca | 2926 | *cmd_result = LTTNG_OK; |
e6887944 JR |
2927 | DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64, |
2928 | trigger_name, trigger_tracer_token); | |
091fa780 | 2929 | goto end; |
505b2d90 | 2930 | |
ab0ee2ca | 2931 | error_free_ht_element: |
242388e4 JR |
2932 | if (trigger_ht_element) { |
2933 | /* Delayed removal due to RCU constraint on delete. */ | |
2934 | call_rcu(&trigger_ht_element->rcu_node, | |
2935 | free_lttng_trigger_ht_element_rcu); | |
2936 | } | |
ab0ee2ca JG |
2937 | error: |
2938 | if (free_trigger) { | |
9c374932 JR |
2939 | /* |
2940 | * Other objects might have a reference to the trigger, mark it | |
2941 | * as unregistered. | |
2942 | */ | |
2943 | lttng_trigger_set_as_unregistered(trigger); | |
ab0ee2ca JG |
2944 | lttng_trigger_destroy(trigger); |
2945 | } | |
091fa780 | 2946 | end: |
ab0ee2ca JG |
2947 | rcu_read_unlock(); |
2948 | return ret; | |
2949 | } | |
2950 | ||
83b934ad MD |
2951 | static |
2952 | void free_lttng_trigger_ht_element_rcu(struct rcu_head *node) | |
2953 | { | |
2954 | free(caa_container_of(node, struct lttng_trigger_ht_element, | |
2955 | rcu_node)); | |
2956 | } | |
2957 | ||
e7c93cf9 JR |
2958 | static |
2959 | void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head *node) | |
2960 | { | |
2961 | free(caa_container_of(node, struct notification_trigger_tokens_ht_element, | |
2962 | rcu_node)); | |
2963 | } | |
2964 | ||
6487ad53 FD |
2965 | static |
2966 | void teardown_tracer_notifier(struct notification_thread_state *state, | |
2967 | const struct lttng_trigger *trigger) | |
2968 | { | |
2969 | struct cds_lfht_iter iter; | |
2970 | struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element; | |
2971 | ||
2972 | cds_lfht_for_each_entry(state->trigger_tokens_ht, &iter, | |
2973 | trigger_tokens_ht_element, node) { | |
2974 | ||
2975 | if (!lttng_trigger_is_equal(trigger, | |
2976 | trigger_tokens_ht_element->trigger)) { | |
2977 | continue; | |
2978 | } | |
2979 | ||
2980 | event_notifier_error_accounting_unregister_event_notifier( | |
2981 | trigger_tokens_ht_element->trigger); | |
2982 | ||
2983 | /* TODO talk to all app and remove it */ | |
2984 | DBG("[notification-thread] Removed trigger from tokens_ht"); | |
2985 | cds_lfht_del(state->trigger_tokens_ht, | |
2986 | &trigger_tokens_ht_element->node); | |
2987 | ||
2988 | call_rcu(&trigger_tokens_ht_element->rcu_node, | |
2989 | free_notification_trigger_tokens_ht_element_rcu); | |
2990 | ||
2991 | break; | |
2992 | } | |
2993 | } | |
2994 | ||
cc2295b5 | 2995 | static |
ab0ee2ca JG |
2996 | int handle_notification_thread_command_unregister_trigger( |
2997 | struct notification_thread_state *state, | |
ac16173e | 2998 | const struct lttng_trigger *trigger, |
ab0ee2ca JG |
2999 | enum lttng_error_code *_cmd_reply) |
3000 | { | |
3001 | struct cds_lfht_iter iter; | |
ed327204 | 3002 | struct cds_lfht_node *triggers_ht_node; |
ab0ee2ca JG |
3003 | struct lttng_channel_trigger_list *trigger_list; |
3004 | struct notification_client_list *client_list; | |
ab0ee2ca | 3005 | struct lttng_trigger_ht_element *trigger_ht_element = NULL; |
ac16173e | 3006 | const struct lttng_condition *condition = lttng_trigger_get_const_condition( |
ab0ee2ca | 3007 | trigger); |
ab0ee2ca JG |
3008 | enum lttng_error_code cmd_reply; |
3009 | ||
3010 | rcu_read_lock(); | |
3011 | ||
3012 | cds_lfht_lookup(state->triggers_ht, | |
3013 | lttng_condition_hash(condition), | |
242388e4 JR |
3014 | match_trigger, |
3015 | trigger, | |
ab0ee2ca JG |
3016 | &iter); |
3017 | triggers_ht_node = cds_lfht_iter_get_node(&iter); | |
3018 | if (!triggers_ht_node) { | |
3019 | cmd_reply = LTTNG_ERR_TRIGGER_NOT_FOUND; | |
3020 | goto end; | |
3021 | } else { | |
3022 | cmd_reply = LTTNG_OK; | |
3023 | } | |
3024 | ||
9c374932 JR |
3025 | trigger_ht_element = caa_container_of(triggers_ht_node, |
3026 | struct lttng_trigger_ht_element, node); | |
3027 | ||
ab0ee2ca JG |
3028 | /* Remove trigger from channel_triggers_ht. */ |
3029 | cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list, | |
3030 | channel_triggers_ht_node) { | |
3031 | struct lttng_trigger_list_element *trigger_element, *tmp; | |
3032 | ||
3033 | cds_list_for_each_entry_safe(trigger_element, tmp, | |
3034 | &trigger_list->list, node) { | |
e8f7fa11 | 3035 | if (!lttng_trigger_is_equal(trigger, trigger_element->trigger)) { |
ab0ee2ca JG |
3036 | continue; |
3037 | } | |
3038 | ||
3039 | DBG("[notification-thread] Removed trigger from channel_triggers_ht"); | |
3040 | cds_list_del(&trigger_element->node); | |
e4db5ace JR |
3041 | /* A trigger can only appear once per channel */ |
3042 | break; | |
ab0ee2ca JG |
3043 | } |
3044 | } | |
3045 | ||
6487ad53 FD |
3046 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
3047 | teardown_tracer_notifier(state, trigger); | |
e7c93cf9 JR |
3048 | } |
3049 | ||
51367634 JR |
3050 | if (is_trigger_action_notify(trigger)) { |
3051 | /* | |
3052 | * Remove and release the client list from | |
3053 | * notification_trigger_clients_ht. | |
3054 | */ | |
3055 | client_list = get_client_list_from_condition(state, condition); | |
3056 | assert(client_list); | |
ed327204 | 3057 | |
091fa780 FD |
3058 | pthread_mutex_lock(&client_list->lock); |
3059 | cds_list_del(&trigger_ht_element->client_list_trigger_node); | |
3060 | pthread_mutex_unlock(&client_list->lock); | |
3061 | ||
51367634 JR |
3062 | /* Put new reference and the hashtable's reference. */ |
3063 | notification_client_list_put(client_list); | |
3064 | notification_client_list_put(client_list); | |
3065 | client_list = NULL; | |
3066 | } | |
ab0ee2ca | 3067 | |
2758e38b FD |
3068 | /* Remove trigger from triggers_ht. */ |
3069 | notif_thread_state_remove_trigger_ht_elem(state, trigger_ht_element); | |
ab0ee2ca | 3070 | |
7ca172c1 | 3071 | /* Release the ownership of the trigger. */ |
ab0ee2ca | 3072 | lttng_trigger_destroy(trigger_ht_element->trigger); |
83b934ad | 3073 | call_rcu(&trigger_ht_element->rcu_node, free_lttng_trigger_ht_element_rcu); |
ab0ee2ca JG |
3074 | end: |
3075 | rcu_read_unlock(); | |
3076 | if (_cmd_reply) { | |
3077 | *_cmd_reply = cmd_reply; | |
3078 | } | |
3079 | return 0; | |
3080 | } | |
3081 | ||
3082 | /* Returns 0 on success, 1 on exit requested, negative value on error. */ | |
3083 | int handle_notification_thread_command( | |
3084 | struct notification_thread_handle *handle, | |
3085 | struct notification_thread_state *state) | |
3086 | { | |
3087 | int ret; | |
3088 | uint64_t counter; | |
3089 | struct notification_thread_command *cmd; | |
3090 | ||
8abe313a | 3091 | /* Read the event pipe to put it back into a quiescent state. */ |
18aeca05 | 3092 | ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter, |
8abe313a | 3093 | sizeof(counter)); |
18aeca05 | 3094 | if (ret != sizeof(counter)) { |
ab0ee2ca JG |
3095 | goto error; |
3096 | } | |
3097 | ||
3098 | pthread_mutex_lock(&handle->cmd_queue.lock); | |
3099 | cmd = cds_list_first_entry(&handle->cmd_queue.list, | |
3100 | struct notification_thread_command, cmd_list_node); | |
97a71ea6 JR |
3101 | cds_list_del(&cmd->cmd_list_node); |
3102 | pthread_mutex_unlock(&handle->cmd_queue.lock); | |
6900ae81 FD |
3103 | |
3104 | DBG("[notification-thread] Received `%s` command", | |
3105 | notification_command_type_str(cmd->type)); | |
ab0ee2ca JG |
3106 | switch (cmd->type) { |
3107 | case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER: | |
ac16173e JG |
3108 | ret = handle_notification_thread_command_register_trigger(state, |
3109 | cmd->parameters.register_trigger.trigger, | |
0efb2ad7 | 3110 | cmd->parameters.register_trigger.is_trigger_anonymous, |
ab0ee2ca JG |
3111 | &cmd->reply_code); |
3112 | break; | |
3113 | case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER: | |
ab0ee2ca | 3114 | ret = handle_notification_thread_command_unregister_trigger( |
ac16173e JG |
3115 | state, |
3116 | cmd->parameters.unregister_trigger.trigger, | |
ab0ee2ca JG |
3117 | &cmd->reply_code); |
3118 | break; | |
3119 | case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL: | |
ab0ee2ca | 3120 | ret = handle_notification_thread_command_add_channel( |
8abe313a JG |
3121 | state, |
3122 | cmd->parameters.add_channel.session.name, | |
3123 | cmd->parameters.add_channel.session.uid, | |
3124 | cmd->parameters.add_channel.session.gid, | |
3125 | cmd->parameters.add_channel.channel.name, | |
3126 | cmd->parameters.add_channel.channel.domain, | |
3127 | cmd->parameters.add_channel.channel.key, | |
3128 | cmd->parameters.add_channel.channel.capacity, | |
ab0ee2ca JG |
3129 | &cmd->reply_code); |
3130 | break; | |
3131 | case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL: | |
ab0ee2ca JG |
3132 | ret = handle_notification_thread_command_remove_channel( |
3133 | state, cmd->parameters.remove_channel.key, | |
3134 | cmd->parameters.remove_channel.domain, | |
3135 | &cmd->reply_code); | |
3136 | break; | |
0ca52944 | 3137 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING: |
0ca52944 | 3138 | case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED: |
ed327204 | 3139 | ret = handle_notification_thread_command_session_rotation( |
0ca52944 | 3140 | state, |
ed327204 JG |
3141 | cmd->type, |
3142 | cmd->parameters.session_rotation.session_name, | |
3143 | cmd->parameters.session_rotation.uid, | |
3144 | cmd->parameters.session_rotation.gid, | |
3145 | cmd->parameters.session_rotation.trace_archive_chunk_id, | |
3146 | cmd->parameters.session_rotation.location, | |
0ca52944 JG |
3147 | &cmd->reply_code); |
3148 | break; | |
d02d7404 JR |
3149 | case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE: |
3150 | ret = handle_notification_thread_command_add_tracer_event_source( | |
3151 | state, | |
3152 | cmd->parameters.tracer_event_source.tracer_event_source_fd, | |
3153 | cmd->parameters.tracer_event_source.domain, | |
3154 | &cmd->reply_code); | |
3155 | break; | |
3156 | case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE: | |
3157 | ret = handle_notification_thread_command_remove_tracer_event_source( | |
3158 | state, | |
3159 | cmd->parameters.tracer_event_source.tracer_event_source_fd, | |
3160 | &cmd->reply_code); | |
3161 | break; | |
fbc9f37d JR |
3162 | case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS: |
3163 | { | |
3164 | struct lttng_triggers *triggers = NULL; | |
3165 | ||
3166 | ret = handle_notification_thread_command_list_triggers( | |
3167 | handle, | |
3168 | state, | |
3169 | cmd->parameters.list_triggers.uid, | |
3170 | &triggers, | |
3171 | &cmd->reply_code); | |
3172 | cmd->reply.list_triggers.triggers = triggers; | |
3173 | ret = 0; | |
3174 | break; | |
3175 | } | |
ab0ee2ca | 3176 | case NOTIFICATION_COMMAND_TYPE_QUIT: |
ab0ee2ca JG |
3177 | cmd->reply_code = LTTNG_OK; |
3178 | ret = 1; | |
3179 | goto end; | |
8790759c FD |
3180 | case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER: |
3181 | { | |
3182 | struct lttng_trigger *trigger = NULL; | |
3183 | ||
3184 | ret = handle_notification_thread_command_get_trigger(state, | |
3185 | cmd->parameters.get_trigger.trigger, &trigger, | |
3186 | &cmd->reply_code); | |
3187 | cmd->reply.get_trigger.trigger = trigger; | |
3188 | break; | |
3189 | } | |
f2b3ef9f JG |
3190 | case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE: |
3191 | { | |
3192 | const enum client_transmission_status client_status = | |
3193 | cmd->parameters.client_communication_update | |
3194 | .status; | |
3195 | const notification_client_id client_id = | |
3196 | cmd->parameters.client_communication_update.id; | |
3197 | struct notification_client *client; | |
3198 | ||
3199 | rcu_read_lock(); | |
3200 | client = get_client_from_id(client_id, state); | |
3201 | ||
3202 | if (!client) { | |
3203 | /* | |
3204 | * Client error was probably already picked-up by the | |
3205 | * notification thread or it has disconnected | |
3206 | * gracefully while this command was queued. | |
3207 | */ | |
3208 | DBG("Failed to find notification client to update communication status, client id = %" PRIu64, | |
3209 | client_id); | |
3210 | ret = 0; | |
3211 | } else { | |
f2b3ef9f JG |
3212 | ret = client_handle_transmission_status( |
3213 | client, client_status, state); | |
f2b3ef9f JG |
3214 | } |
3215 | rcu_read_unlock(); | |
3216 | break; | |
3217 | } | |
ab0ee2ca JG |
3218 | default: |
3219 | ERR("[notification-thread] Unknown internal command received"); | |
3220 | goto error_unlock; | |
3221 | } | |
3222 | ||
3223 | if (ret) { | |
3224 | goto error_unlock; | |
3225 | } | |
3226 | end: | |
0ab399e0 JG |
3227 | if (cmd->is_async) { |
3228 | free(cmd); | |
3229 | cmd = NULL; | |
3230 | } else { | |
3231 | lttng_waiter_wake_up(&cmd->reply_waiter); | |
3232 | } | |
ab0ee2ca JG |
3233 | return ret; |
3234 | error_unlock: | |
3235 | /* Wake-up and return a fatal error to the calling thread. */ | |
8ada111f | 3236 | lttng_waiter_wake_up(&cmd->reply_waiter); |
ab0ee2ca JG |
3237 | cmd->reply_code = LTTNG_ERR_FATAL; |
3238 | error: | |
3239 | /* Indicate a fatal error to the caller. */ | |
3240 | return -1; | |
3241 | } | |
3242 | ||
ab0ee2ca JG |
3243 | static |
3244 | int socket_set_non_blocking(int socket) | |
3245 | { | |
3246 | int ret, flags; | |
3247 | ||
3248 | /* Set the pipe as non-blocking. */ | |
3249 | ret = fcntl(socket, F_GETFL, 0); | |
3250 | if (ret == -1) { | |
3251 | PERROR("fcntl get socket flags"); | |
3252 | goto end; | |
3253 | } | |
3254 | flags = ret; | |
3255 | ||
3256 | ret = fcntl(socket, F_SETFL, flags | O_NONBLOCK); | |
3257 | if (ret == -1) { | |
3258 | PERROR("fcntl set O_NONBLOCK socket flag"); | |
3259 | goto end; | |
3260 | } | |
3261 | DBG("Client socket (fd = %i) set as non-blocking", socket); | |
3262 | end: | |
3263 | return ret; | |
3264 | } | |
3265 | ||
3266 | static | |
14fa22f8 | 3267 | int client_reset_inbound_state(struct notification_client *client) |
ab0ee2ca JG |
3268 | { |
3269 | int ret; | |
3270 | ||
882093ee JR |
3271 | |
3272 | lttng_payload_clear(&client->communication.inbound.payload); | |
ab0ee2ca JG |
3273 | |
3274 | client->communication.inbound.bytes_to_receive = | |
3275 | sizeof(struct lttng_notification_channel_message); | |
3276 | client->communication.inbound.msg_type = | |
3277 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN; | |
ab0ee2ca JG |
3278 | LTTNG_SOCK_SET_UID_CRED(&client->communication.inbound.creds, -1); |
3279 | LTTNG_SOCK_SET_GID_CRED(&client->communication.inbound.creds, -1); | |
14fa22f8 | 3280 | ret = lttng_dynamic_buffer_set_size( |
882093ee | 3281 | &client->communication.inbound.payload.buffer, |
14fa22f8 | 3282 | client->communication.inbound.bytes_to_receive); |
882093ee | 3283 | |
14fa22f8 | 3284 | return ret; |
ab0ee2ca JG |
3285 | } |
3286 | ||
3287 | int handle_notification_thread_client_connect( | |
3288 | struct notification_thread_state *state) | |
3289 | { | |
3290 | int ret; | |
3291 | struct notification_client *client; | |
3292 | ||
3293 | DBG("[notification-thread] Handling new notification channel client connection"); | |
3294 | ||
3295 | client = zmalloc(sizeof(*client)); | |
3296 | if (!client) { | |
3297 | /* Fatal error. */ | |
3298 | ret = -1; | |
3299 | goto error; | |
3300 | } | |
6c24d3fd | 3301 | |
505b2d90 | 3302 | pthread_mutex_init(&client->lock, NULL); |
ac1889bf | 3303 | client->id = state->next_notification_client_id++; |
ab0ee2ca | 3304 | CDS_INIT_LIST_HEAD(&client->condition_list); |
882093ee JR |
3305 | lttng_payload_init(&client->communication.inbound.payload); |
3306 | lttng_payload_init(&client->communication.outbound.payload); | |
01ea340e | 3307 | client->communication.inbound.expect_creds = true; |
505b2d90 | 3308 | |
14fa22f8 JG |
3309 | ret = client_reset_inbound_state(client); |
3310 | if (ret) { | |
3311 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
e742b055 | 3312 | ret = 0; |
14fa22f8 JG |
3313 | goto error; |
3314 | } | |
ab0ee2ca JG |
3315 | |
3316 | ret = lttcomm_accept_unix_sock(state->notification_channel_socket); | |
3317 | if (ret < 0) { | |
3318 | ERR("[notification-thread] Failed to accept new notification channel client connection"); | |
3319 | ret = 0; | |
3320 | goto error; | |
3321 | } | |
3322 | ||
3323 | client->socket = ret; | |
3324 | ||
3325 | ret = socket_set_non_blocking(client->socket); | |
3326 | if (ret) { | |
3327 | ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking"); | |
3328 | goto error; | |
3329 | } | |
3330 | ||
3331 | ret = lttcomm_setsockopt_creds_unix_sock(client->socket); | |
3332 | if (ret < 0) { | |
3333 | ERR("[notification-thread] Failed to set socket options on new notification channel client socket"); | |
3334 | ret = 0; | |
3335 | goto error; | |
3336 | } | |
3337 | ||
3338 | ret = lttng_poll_add(&state->events, client->socket, | |
3339 | LPOLLIN | LPOLLERR | | |
3340 | LPOLLHUP | LPOLLRDHUP); | |
3341 | if (ret < 0) { | |
3342 | ERR("[notification-thread] Failed to add notification channel client socket to poll set"); | |
3343 | ret = 0; | |
3344 | goto error; | |
3345 | } | |
3346 | DBG("[notification-thread] Added new notification channel client socket (%i) to poll set", | |
3347 | client->socket); | |
3348 | ||
ab0ee2ca JG |
3349 | rcu_read_lock(); |
3350 | cds_lfht_add(state->client_socket_ht, | |
3351 | hash_client_socket(client->socket), | |
3352 | &client->client_socket_ht_node); | |
ac1889bf JG |
3353 | cds_lfht_add(state->client_id_ht, |
3354 | hash_client_id(client->id), | |
3355 | &client->client_id_ht_node); | |
ab0ee2ca JG |
3356 | rcu_read_unlock(); |
3357 | ||
3358 | return ret; | |
6c24d3fd | 3359 | |
ab0ee2ca JG |
3360 | error: |
3361 | notification_client_destroy(client, state); | |
3362 | return ret; | |
3363 | } | |
3364 | ||
6c24d3fd JG |
3365 | /* |
3366 | * RCU read-lock must be held by the caller. | |
3367 | * Client lock must _not_ be held by the caller. | |
3368 | */ | |
505b2d90 JG |
3369 | static |
3370 | int notification_thread_client_disconnect( | |
3371 | struct notification_client *client, | |
ab0ee2ca | 3372 | struct notification_thread_state *state) |
505b2d90 JG |
3373 | { |
3374 | int ret; | |
3375 | struct lttng_condition_list_element *condition_list_element, *tmp; | |
3376 | ||
3377 | /* Acquire the client lock to disable its communication atomically. */ | |
6c24d3fd | 3378 | pthread_mutex_lock(&client->lock); |
505b2d90 | 3379 | client->communication.active = false; |
6c24d3fd JG |
3380 | cds_lfht_del(state->client_socket_ht, &client->client_socket_ht_node); |
3381 | cds_lfht_del(state->client_id_ht, &client->client_id_ht_node); | |
3382 | pthread_mutex_unlock(&client->lock); | |
3383 | ||
505b2d90 JG |
3384 | ret = lttng_poll_del(&state->events, client->socket); |
3385 | if (ret) { | |
3386 | ERR("[notification-thread] Failed to remove client socket %d from poll set", | |
3387 | client->socket); | |
3388 | } | |
3389 | ||
505b2d90 JG |
3390 | /* Release all conditions to which the client was subscribed. */ |
3391 | cds_list_for_each_entry_safe(condition_list_element, tmp, | |
3392 | &client->condition_list, node) { | |
3393 | (void) notification_thread_client_unsubscribe(client, | |
3394 | condition_list_element->condition, state, NULL); | |
3395 | } | |
3396 | ||
3397 | /* | |
3398 | * Client no longer accessible to other threads (through the | |
3399 | * client lists). | |
3400 | */ | |
3401 | notification_client_destroy(client, state); | |
3402 | return ret; | |
3403 | } | |
3404 | ||
3405 | int handle_notification_thread_client_disconnect( | |
3406 | int client_socket, struct notification_thread_state *state) | |
ab0ee2ca JG |
3407 | { |
3408 | int ret = 0; | |
3409 | struct notification_client *client; | |
3410 | ||
3411 | rcu_read_lock(); | |
3412 | DBG("[notification-thread] Closing client connection (socket fd = %i)", | |
3413 | client_socket); | |
3414 | client = get_client_from_socket(client_socket, state); | |
3415 | if (!client) { | |
3416 | /* Internal state corruption, fatal error. */ | |
3417 | ERR("[notification-thread] Unable to find client (socket fd = %i)", | |
3418 | client_socket); | |
3419 | ret = -1; | |
3420 | goto end; | |
3421 | } | |
3422 | ||
505b2d90 | 3423 | ret = notification_thread_client_disconnect(client, state); |
ab0ee2ca JG |
3424 | end: |
3425 | rcu_read_unlock(); | |
3426 | return ret; | |
3427 | } | |
3428 | ||
3429 | int handle_notification_thread_client_disconnect_all( | |
3430 | struct notification_thread_state *state) | |
3431 | { | |
3432 | struct cds_lfht_iter iter; | |
3433 | struct notification_client *client; | |
3434 | bool error_encoutered = false; | |
3435 | ||
3436 | rcu_read_lock(); | |
3437 | DBG("[notification-thread] Closing all client connections"); | |
3438 | cds_lfht_for_each_entry(state->client_socket_ht, &iter, client, | |
505b2d90 | 3439 | client_socket_ht_node) { |
ab0ee2ca JG |
3440 | int ret; |
3441 | ||
505b2d90 JG |
3442 | ret = notification_thread_client_disconnect( |
3443 | client, state); | |
ab0ee2ca JG |
3444 | if (ret) { |
3445 | error_encoutered = true; | |
3446 | } | |
3447 | } | |
3448 | rcu_read_unlock(); | |
3449 | return error_encoutered ? 1 : 0; | |
3450 | } | |
3451 | ||
3452 | int handle_notification_thread_trigger_unregister_all( | |
3453 | struct notification_thread_state *state) | |
3454 | { | |
4149ace8 | 3455 | bool error_occurred = false; |
ab0ee2ca JG |
3456 | struct cds_lfht_iter iter; |
3457 | struct lttng_trigger_ht_element *trigger_ht_element; | |
3458 | ||
763de384 | 3459 | rcu_read_lock(); |
ab0ee2ca JG |
3460 | cds_lfht_for_each_entry(state->triggers_ht, &iter, trigger_ht_element, |
3461 | node) { | |
3462 | int ret = handle_notification_thread_command_unregister_trigger( | |
3463 | state, trigger_ht_element->trigger, NULL); | |
3464 | if (ret) { | |
4149ace8 | 3465 | error_occurred = true; |
ab0ee2ca JG |
3466 | } |
3467 | } | |
763de384 | 3468 | rcu_read_unlock(); |
4149ace8 | 3469 | return error_occurred ? -1 : 0; |
ab0ee2ca JG |
3470 | } |
3471 | ||
dd877ea6 JG |
3472 | static |
3473 | int client_handle_transmission_status( | |
3474 | struct notification_client *client, | |
3475 | enum client_transmission_status transmission_status, | |
3476 | struct notification_thread_state *state) | |
3477 | { | |
3478 | int ret = 0; | |
3479 | ||
3480 | switch (transmission_status) { | |
3481 | case CLIENT_TRANSMISSION_STATUS_COMPLETE: | |
3482 | ret = lttng_poll_mod(&state->events, client->socket, | |
3483 | CLIENT_POLL_MASK_IN); | |
3484 | if (ret) { | |
3485 | goto end; | |
3486 | } | |
3487 | ||
dd877ea6 JG |
3488 | break; |
3489 | case CLIENT_TRANSMISSION_STATUS_QUEUED: | |
3490 | /* | |
3491 | * We want to be notified whenever there is buffer space | |
3492 | * available to send the rest of the payload. | |
3493 | */ | |
3494 | ret = lttng_poll_mod(&state->events, client->socket, | |
3495 | CLIENT_POLL_MASK_IN_OUT); | |
3496 | if (ret) { | |
3497 | goto end; | |
3498 | } | |
3499 | break; | |
3500 | case CLIENT_TRANSMISSION_STATUS_FAIL: | |
3501 | ret = notification_thread_client_disconnect(client, state); | |
3502 | if (ret) { | |
3503 | goto end; | |
3504 | } | |
3505 | break; | |
3506 | case CLIENT_TRANSMISSION_STATUS_ERROR: | |
3507 | ret = -1; | |
3508 | goto end; | |
3509 | default: | |
3510 | abort(); | |
3511 | } | |
3512 | end: | |
3513 | return ret; | |
3514 | } | |
3515 | ||
505b2d90 | 3516 | /* Client lock must be acquired by caller. */ |
ab0ee2ca | 3517 | static |
dd877ea6 | 3518 | enum client_transmission_status client_flush_outgoing_queue( |
f2b3ef9f | 3519 | struct notification_client *client) |
ab0ee2ca JG |
3520 | { |
3521 | ssize_t ret; | |
3522 | size_t to_send_count; | |
dd877ea6 | 3523 | enum client_transmission_status status; |
882093ee JR |
3524 | struct lttng_payload_view pv = lttng_payload_view_from_payload( |
3525 | &client->communication.outbound.payload, 0, -1); | |
3526 | const int fds_to_send_count = | |
3527 | lttng_payload_view_get_fd_handle_count(&pv); | |
ab0ee2ca | 3528 | |
505b2d90 JG |
3529 | ASSERT_LOCKED(client->lock); |
3530 | ||
f2b3ef9f JG |
3531 | if (!client->communication.active) { |
3532 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3533 | goto end; | |
3534 | } | |
3535 | ||
882093ee JR |
3536 | if (pv.buffer.size == 0) { |
3537 | /* | |
3538 | * If both data and fds are equal to zero, we are in an invalid | |
3539 | * state. | |
3540 | */ | |
3541 | assert(fds_to_send_count != 0); | |
3542 | goto send_fds; | |
3543 | } | |
3544 | ||
3545 | /* Send data. */ | |
3546 | to_send_count = pv.buffer.size; | |
ab0ee2ca JG |
3547 | DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue", |
3548 | client->socket); | |
3549 | ||
3550 | ret = lttcomm_send_unix_sock_non_block(client->socket, | |
882093ee | 3551 | pv.buffer.data, |
ab0ee2ca | 3552 | to_send_count); |
44c180ca | 3553 | if ((ret >= 0 && ret < to_send_count)) { |
ab0ee2ca JG |
3554 | DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed", |
3555 | client->socket); | |
3556 | to_send_count -= max(ret, 0); | |
3557 | ||
6f110534 | 3558 | memmove(client->communication.outbound.payload.buffer.data, |
882093ee JR |
3559 | pv.buffer.data + |
3560 | pv.buffer.size - to_send_count, | |
ab0ee2ca JG |
3561 | to_send_count); |
3562 | ret = lttng_dynamic_buffer_set_size( | |
882093ee | 3563 | &client->communication.outbound.payload.buffer, |
ab0ee2ca JG |
3564 | to_send_count); |
3565 | if (ret) { | |
3566 | goto error; | |
3567 | } | |
6c24d3fd | 3568 | |
dd877ea6 | 3569 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; |
882093ee | 3570 | goto end; |
ab0ee2ca | 3571 | } else if (ret < 0) { |
6c24d3fd | 3572 | /* Generic error, disable the client's communication. */ |
dd877ea6 | 3573 | ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)", |
ab0ee2ca | 3574 | client->socket); |
6c24d3fd | 3575 | client->communication.active = false; |
dd877ea6 | 3576 | status = CLIENT_TRANSMISSION_STATUS_FAIL; |
882093ee | 3577 | goto end; |
ab0ee2ca | 3578 | } else { |
882093ee JR |
3579 | /* |
3580 | * No error and flushed the queue completely. | |
3581 | * | |
3582 | * The payload buffer size is used later to | |
3583 | * check if there is notifications queued. So albeit that the | |
3584 | * direct caller knows that the transmission is complete, we | |
3585 | * need to set the buffer size to zero. | |
3586 | */ | |
ab0ee2ca | 3587 | ret = lttng_dynamic_buffer_set_size( |
882093ee | 3588 | &client->communication.outbound.payload.buffer, 0); |
ab0ee2ca JG |
3589 | if (ret) { |
3590 | goto error; | |
3591 | } | |
882093ee | 3592 | } |
6c24d3fd | 3593 | |
882093ee JR |
3594 | send_fds: |
3595 | /* No fds to send, transmission is complete. */ | |
3596 | if (fds_to_send_count == 0) { | |
dd877ea6 | 3597 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; |
882093ee | 3598 | goto end; |
dd877ea6 | 3599 | } |
882093ee JR |
3600 | |
3601 | ret = lttcomm_send_payload_view_fds_unix_sock_non_block( | |
3602 | client->socket, &pv); | |
3603 | if (ret < 0) { | |
3604 | /* Generic error, disable the client's communication. */ | |
3605 | ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)", | |
3606 | client->socket); | |
3607 | client->communication.active = false; | |
3608 | status = CLIENT_TRANSMISSION_STATUS_FAIL; | |
3609 | goto end; | |
3610 | } else if (ret == 0) { | |
3611 | /* Nothing could be sent. */ | |
3612 | status = CLIENT_TRANSMISSION_STATUS_QUEUED; | |
3613 | } else { | |
3614 | /* Fd passing is an all or nothing kind of thing. */ | |
3615 | status = CLIENT_TRANSMISSION_STATUS_COMPLETE; | |
3616 | /* | |
3617 | * The payload _fd_array count is used later to | |
3618 | * check if there is notifications queued. So although the | |
3619 | * direct caller knows that the transmission is complete, we | |
3620 | * need to clear the _fd_array for the queuing check. | |
3621 | */ | |
3622 | lttng_dynamic_pointer_array_clear( | |
3623 | &client->communication.outbound.payload | |
3624 | ._fd_handles); | |
3625 | } | |
3626 | ||
f2b3ef9f | 3627 | end: |
882093ee JR |
3628 | if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) { |
3629 | client->communication.outbound.queued_command_reply = false; | |
3630 | client->communication.outbound.dropped_notification = false; | |
3631 | lttng_payload_clear(&client->communication.outbound.payload); | |
3632 | } | |
3633 | ||
f2b3ef9f | 3634 | return status; |
ab0ee2ca | 3635 | error: |
f2b3ef9f | 3636 | return CLIENT_TRANSMISSION_STATUS_ERROR; |
ab0ee2ca JG |
3637 | } |
3638 | ||
882093ee JR |
3639 | static |
3640 | bool client_has_outbound_data_left( | |
3641 | const struct notification_client *client) | |
3642 | { | |
3643 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( | |
3644 | &client->communication.outbound.payload, 0, -1); | |
3645 | const bool has_data = pv.buffer.size != 0; | |
3646 | const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv); | |
3647 | ||
3648 | return has_data || has_fds; | |
3649 | } | |
3650 | ||
6c24d3fd | 3651 | /* Client lock must _not_ be held by the caller. */ |
ab0ee2ca JG |
3652 | static |
3653 | int client_send_command_reply(struct notification_client *client, | |
3654 | struct notification_thread_state *state, | |
3655 | enum lttng_notification_channel_status status) | |
3656 | { | |
3657 | int ret; | |
3658 | struct lttng_notification_channel_command_reply reply = { | |
3659 | .status = (int8_t) status, | |
3660 | }; | |
3661 | struct lttng_notification_channel_message msg = { | |
3662 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY, | |
3663 | .size = sizeof(reply), | |
3664 | }; | |
3665 | char buffer[sizeof(msg) + sizeof(reply)]; | |
f2b3ef9f | 3666 | enum client_transmission_status transmission_status; |
ab0ee2ca | 3667 | |
6c24d3fd JG |
3668 | memcpy(buffer, &msg, sizeof(msg)); |
3669 | memcpy(buffer + sizeof(msg), &reply, sizeof(reply)); | |
3670 | DBG("[notification-thread] Send command reply (%i)", (int) status); | |
505b2d90 | 3671 | |
6c24d3fd | 3672 | pthread_mutex_lock(&client->lock); |
ab0ee2ca JG |
3673 | if (client->communication.outbound.queued_command_reply) { |
3674 | /* Protocol error. */ | |
6c24d3fd | 3675 | goto error_unlock; |
ab0ee2ca JG |
3676 | } |
3677 | ||
ab0ee2ca JG |
3678 | /* Enqueue buffer to outgoing queue and flush it. */ |
3679 | ret = lttng_dynamic_buffer_append( | |
882093ee | 3680 | &client->communication.outbound.payload.buffer, |
ab0ee2ca JG |
3681 | buffer, sizeof(buffer)); |
3682 | if (ret) { | |
6c24d3fd | 3683 | goto error_unlock; |
ab0ee2ca JG |
3684 | } |
3685 | ||
f2b3ef9f | 3686 | transmission_status = client_flush_outgoing_queue(client); |
882093ee JR |
3687 | |
3688 | if (client_has_outbound_data_left(client)) { | |
6c24d3fd JG |
3689 | /* Queue could not be emptied. */ |
3690 | client->communication.outbound.queued_command_reply = true; | |
3691 | } | |
3692 | ||
3693 | pthread_mutex_unlock(&client->lock); | |
f2b3ef9f JG |
3694 | ret = client_handle_transmission_status( |
3695 | client, transmission_status, state); | |
ab0ee2ca JG |
3696 | if (ret) { |
3697 | goto error; | |
3698 | } | |
3699 | ||
ab0ee2ca | 3700 | return 0; |
6c24d3fd JG |
3701 | error_unlock: |
3702 | pthread_mutex_unlock(&client->lock); | |
ab0ee2ca JG |
3703 | error: |
3704 | return -1; | |
3705 | } | |
3706 | ||
505b2d90 JG |
3707 | static |
3708 | int client_handle_message_unknown(struct notification_client *client, | |
3709 | struct notification_thread_state *state) | |
3710 | { | |
3711 | int ret; | |
505b2d90 JG |
3712 | /* |
3713 | * Receiving message header. The function will be called again | |
3714 | * once the rest of the message as been received and can be | |
3715 | * interpreted. | |
3716 | */ | |
3717 | const struct lttng_notification_channel_message *msg; | |
3718 | ||
882093ee | 3719 | assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size); |
505b2d90 | 3720 | msg = (const struct lttng_notification_channel_message *) |
882093ee | 3721 | client->communication.inbound.payload.buffer.data; |
505b2d90 JG |
3722 | |
3723 | if (msg->size == 0 || | |
3724 | msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) { | |
3725 | ERR("[notification-thread] Invalid notification channel message: length = %u", | |
3726 | msg->size); | |
3727 | ret = -1; | |
3728 | goto end; | |
3729 | } | |
3730 | ||
3731 | switch (msg->type) { | |
3732 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3733 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3734 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3735 | break; | |
3736 | default: | |
3737 | ret = -1; | |
3738 | ERR("[notification-thread] Invalid notification channel message: unexpected message type"); | |
3739 | goto end; | |
3740 | } | |
3741 | ||
3742 | client->communication.inbound.bytes_to_receive = msg->size; | |
882093ee | 3743 | client->communication.inbound.fds_to_receive = msg->fds; |
505b2d90 JG |
3744 | client->communication.inbound.msg_type = |
3745 | (enum lttng_notification_channel_message_type) msg->type; | |
3746 | ret = lttng_dynamic_buffer_set_size( | |
882093ee JR |
3747 | &client->communication.inbound.payload.buffer, msg->size); |
3748 | ||
3749 | /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */ | |
3750 | msg = NULL; | |
505b2d90 | 3751 | end: |
505b2d90 JG |
3752 | return ret; |
3753 | } | |
3754 | ||
3755 | static | |
3756 | int client_handle_message_handshake(struct notification_client *client, | |
3757 | struct notification_thread_state *state) | |
3758 | { | |
3759 | int ret; | |
3760 | struct lttng_notification_channel_command_handshake *handshake_client; | |
3761 | const struct lttng_notification_channel_command_handshake handshake_reply = { | |
3762 | .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR, | |
3763 | .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR, | |
3764 | }; | |
3765 | const struct lttng_notification_channel_message msg_header = { | |
3766 | .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE, | |
3767 | .size = sizeof(handshake_reply), | |
3768 | }; | |
3769 | enum lttng_notification_channel_status status = | |
3770 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
3771 | char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)]; | |
505b2d90 JG |
3772 | |
3773 | memcpy(send_buffer, &msg_header, sizeof(msg_header)); | |
3774 | memcpy(send_buffer + sizeof(msg_header), &handshake_reply, | |
3775 | sizeof(handshake_reply)); | |
3776 | ||
3777 | handshake_client = | |
3778 | (struct lttng_notification_channel_command_handshake *) | |
882093ee | 3779 | client->communication.inbound.payload.buffer |
505b2d90 JG |
3780 | .data; |
3781 | client->major = handshake_client->major; | |
3782 | client->minor = handshake_client->minor; | |
3783 | if (!client->communication.inbound.creds_received) { | |
3784 | ERR("[notification-thread] No credentials received from client"); | |
3785 | ret = -1; | |
3786 | goto end; | |
3787 | } | |
3788 | ||
3789 | client->uid = LTTNG_SOCK_GET_UID_CRED( | |
3790 | &client->communication.inbound.creds); | |
3791 | client->gid = LTTNG_SOCK_GET_GID_CRED( | |
3792 | &client->communication.inbound.creds); | |
3793 | DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i", | |
3794 | client->uid, client->gid, (int) client->major, | |
3795 | (int) client->minor); | |
3796 | ||
3797 | if (handshake_client->major != | |
3798 | LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) { | |
3799 | status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION; | |
3800 | } | |
3801 | ||
6c24d3fd JG |
3802 | pthread_mutex_lock(&client->lock); |
3803 | /* Outgoing queue will be flushed when the command reply is sent. */ | |
505b2d90 | 3804 | ret = lttng_dynamic_buffer_append( |
882093ee | 3805 | &client->communication.outbound.payload.buffer, send_buffer, |
505b2d90 JG |
3806 | sizeof(send_buffer)); |
3807 | if (ret) { | |
3808 | ERR("[notification-thread] Failed to send protocol version to notification channel client"); | |
6c24d3fd | 3809 | goto end_unlock; |
505b2d90 JG |
3810 | } |
3811 | ||
3812 | client->validated = true; | |
3813 | client->communication.active = true; | |
6c24d3fd | 3814 | pthread_mutex_unlock(&client->lock); |
505b2d90 | 3815 | |
6c24d3fd JG |
3816 | /* Set reception state to receive the next message header. */ |
3817 | ret = client_reset_inbound_state(client); | |
505b2d90 | 3818 | if (ret) { |
6c24d3fd | 3819 | ERR("[notification-thread] Failed to reset client communication's inbound state"); |
505b2d90 JG |
3820 | goto end; |
3821 | } | |
3822 | ||
6c24d3fd | 3823 | /* Flushes the outgoing queue. */ |
505b2d90 JG |
3824 | ret = client_send_command_reply(client, state, status); |
3825 | if (ret) { | |
3826 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
3827 | goto end; | |
3828 | } | |
3829 | ||
6c24d3fd JG |
3830 | goto end; |
3831 | end_unlock: | |
505b2d90 | 3832 | pthread_mutex_unlock(&client->lock); |
6c24d3fd | 3833 | end: |
505b2d90 JG |
3834 | return ret; |
3835 | } | |
3836 | ||
3837 | static | |
3838 | int client_handle_message_subscription( | |
3839 | struct notification_client *client, | |
3840 | enum lttng_notification_channel_message_type msg_type, | |
3841 | struct notification_thread_state *state) | |
3842 | { | |
3843 | int ret; | |
3844 | struct lttng_condition *condition; | |
3845 | enum lttng_notification_channel_status status = | |
3846 | LTTNG_NOTIFICATION_CHANNEL_STATUS_OK; | |
3847 | struct lttng_payload_view condition_view = | |
882093ee JR |
3848 | lttng_payload_view_from_payload( |
3849 | &client->communication.inbound.payload, | |
505b2d90 JG |
3850 | 0, -1); |
3851 | size_t expected_condition_size; | |
3852 | ||
6c24d3fd JG |
3853 | /* |
3854 | * No need to lock client to sample the inbound state as the only | |
3855 | * other thread accessing clients (action executor) only uses the | |
3856 | * outbound state. | |
3857 | */ | |
882093ee | 3858 | expected_condition_size = client->communication.inbound.payload.buffer.size; |
505b2d90 JG |
3859 | ret = lttng_condition_create_from_payload(&condition_view, &condition); |
3860 | if (ret != expected_condition_size) { | |
3861 | ERR("[notification-thread] Malformed condition received from client"); | |
3862 | goto end; | |
3863 | } | |
3864 | ||
3865 | if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) { | |
3866 | ret = notification_thread_client_subscribe( | |
3867 | client, condition, state, &status); | |
3868 | } else { | |
3869 | ret = notification_thread_client_unsubscribe( | |
3870 | client, condition, state, &status); | |
3871 | } | |
505b2d90 | 3872 | |
505b2d90 | 3873 | if (ret) { |
6c24d3fd | 3874 | goto end; |
505b2d90 JG |
3875 | } |
3876 | ||
3877 | /* Set reception state to receive the next message header. */ | |
3878 | ret = client_reset_inbound_state(client); | |
3879 | if (ret) { | |
3880 | ERR("[notification-thread] Failed to reset client communication's inbound state"); | |
6c24d3fd JG |
3881 | goto end; |
3882 | } | |
3883 | ||
3884 | ret = client_send_command_reply(client, state, status); | |
3885 | if (ret) { | |
3886 | ERR("[notification-thread] Failed to send reply to notification channel client"); | |
3887 | goto end; | |
505b2d90 JG |
3888 | } |
3889 | ||
505b2d90 JG |
3890 | end: |
3891 | return ret; | |
3892 | } | |
3893 | ||
ab0ee2ca JG |
3894 | static |
3895 | int client_dispatch_message(struct notification_client *client, | |
3896 | struct notification_thread_state *state) | |
3897 | { | |
3898 | int ret = 0; | |
3899 | ||
3900 | if (client->communication.inbound.msg_type != | |
3901 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE && | |
3902 | client->communication.inbound.msg_type != | |
3903 | LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN && | |
3904 | !client->validated) { | |
3905 | WARN("[notification-thread] client attempted a command before handshake"); | |
3906 | ret = -1; | |
3907 | goto end; | |
3908 | } | |
3909 | ||
3910 | switch (client->communication.inbound.msg_type) { | |
3911 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN: | |
3912 | { | |
505b2d90 | 3913 | ret = client_handle_message_unknown(client, state); |
ab0ee2ca JG |
3914 | break; |
3915 | } | |
3916 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE: | |
3917 | { | |
505b2d90 | 3918 | ret = client_handle_message_handshake(client, state); |
ab0ee2ca JG |
3919 | break; |
3920 | } | |
3921 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE: | |
3922 | case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE: | |
3923 | { | |
505b2d90 JG |
3924 | ret = client_handle_message_subscription(client, |
3925 | client->communication.inbound.msg_type, state); | |
ab0ee2ca JG |
3926 | break; |
3927 | } | |
3928 | default: | |
3929 | abort(); | |
3930 | } | |
3931 | end: | |
3932 | return ret; | |
3933 | } | |
3934 | ||
3935 | /* Incoming data from client. */ | |
3936 | int handle_notification_thread_client_in( | |
3937 | struct notification_thread_state *state, int socket) | |
3938 | { | |
14fa22f8 | 3939 | int ret = 0; |
ab0ee2ca JG |
3940 | struct notification_client *client; |
3941 | ssize_t recv_ret; | |
3942 | size_t offset; | |
3943 | ||
98b5ff34 | 3944 | rcu_read_lock(); |
ab0ee2ca JG |
3945 | client = get_client_from_socket(socket, state); |
3946 | if (!client) { | |
3947 | /* Internal error, abort. */ | |
3948 | ret = -1; | |
3949 | goto end; | |
3950 | } | |
3951 | ||
882093ee | 3952 | offset = client->communication.inbound.payload.buffer.size - |
14fa22f8 | 3953 | client->communication.inbound.bytes_to_receive; |
01ea340e | 3954 | if (client->communication.inbound.expect_creds) { |
ab0ee2ca | 3955 | recv_ret = lttcomm_recv_creds_unix_sock(socket, |
882093ee | 3956 | client->communication.inbound.payload.buffer.data + offset, |
ab0ee2ca JG |
3957 | client->communication.inbound.bytes_to_receive, |
3958 | &client->communication.inbound.creds); | |
3959 | if (recv_ret > 0) { | |
01ea340e | 3960 | client->communication.inbound.expect_creds = false; |
ab0ee2ca JG |
3961 | client->communication.inbound.creds_received = true; |
3962 | } | |
3963 | } else { | |
3964 | recv_ret = lttcomm_recv_unix_sock_non_block(socket, | |
882093ee | 3965 | client->communication.inbound.payload.buffer.data + offset, |
ab0ee2ca JG |
3966 | client->communication.inbound.bytes_to_receive); |
3967 | } | |
505b2d90 JG |
3968 | if (recv_ret >= 0) { |
3969 | client->communication.inbound.bytes_to_receive -= recv_ret; | |
882093ee JR |
3970 | } else { |
3971 | goto error_disconnect_client; | |
505b2d90 | 3972 | } |
6c24d3fd | 3973 | |
882093ee JR |
3974 | if (client->communication.inbound.bytes_to_receive != 0) { |
3975 | /* Message incomplete wait for more data. */ | |
3976 | ret = 0; | |
3977 | goto end; | |
ab0ee2ca JG |
3978 | } |
3979 | ||
882093ee JR |
3980 | assert(client->communication.inbound.bytes_to_receive == 0); |
3981 | ||
3982 | /* Receive fds. */ | |
3983 | if (client->communication.inbound.fds_to_receive != 0) { | |
3984 | ret = lttcomm_recv_payload_fds_unix_sock_non_block( | |
3985 | client->socket, | |
3986 | client->communication.inbound.fds_to_receive, | |
3987 | &client->communication.inbound.payload); | |
3988 | if (ret > 0) { | |
ab0ee2ca | 3989 | /* |
882093ee JR |
3990 | * Fds received. non blocking fds passing is all |
3991 | * or nothing. | |
ab0ee2ca | 3992 | */ |
882093ee JR |
3993 | ssize_t expected_size; |
3994 | ||
3995 | expected_size = sizeof(int) * | |
3996 | client->communication.inbound | |
3997 | .fds_to_receive; | |
3998 | assert(ret == expected_size); | |
3999 | client->communication.inbound.fds_to_receive = 0; | |
4000 | } else if (ret == 0) { | |
4001 | /* Received nothing. */ | |
4002 | ret = 0; | |
4003 | goto end; | |
4004 | } else { | |
ab0ee2ca JG |
4005 | goto error_disconnect_client; |
4006 | } | |
ab0ee2ca | 4007 | } |
882093ee JR |
4008 | |
4009 | /* At this point the message is complete.*/ | |
4010 | assert(client->communication.inbound.bytes_to_receive == 0 && | |
4011 | client->communication.inbound.fds_to_receive == 0); | |
4012 | ret = client_dispatch_message(client, state); | |
4013 | if (ret) { | |
4014 | /* | |
4015 | * Only returns an error if this client must be | |
4016 | * disconnected. | |
4017 | */ | |
4018 | goto error_disconnect_client; | |
4019 | } | |
4020 | ||
ab0ee2ca | 4021 | end: |
98b5ff34 | 4022 | rcu_read_unlock(); |
ab0ee2ca | 4023 | return ret; |
882093ee | 4024 | |
ab0ee2ca | 4025 | error_disconnect_client: |
505b2d90 | 4026 | ret = notification_thread_client_disconnect(client, state); |
98b5ff34 | 4027 | goto end; |
ab0ee2ca JG |
4028 | } |
4029 | ||
4030 | /* Client ready to receive outgoing data. */ | |
4031 | int handle_notification_thread_client_out( | |
4032 | struct notification_thread_state *state, int socket) | |
4033 | { | |
4034 | int ret; | |
4035 | struct notification_client *client; | |
f2b3ef9f | 4036 | enum client_transmission_status transmission_status; |
ab0ee2ca | 4037 | |
98b5ff34 | 4038 | rcu_read_lock(); |
ab0ee2ca JG |
4039 | client = get_client_from_socket(socket, state); |
4040 | if (!client) { | |
4041 | /* Internal error, abort. */ | |
4042 | ret = -1; | |
4043 | goto end; | |
4044 | } | |
4045 | ||
505b2d90 | 4046 | pthread_mutex_lock(&client->lock); |
f2b3ef9f | 4047 | transmission_status = client_flush_outgoing_queue(client); |
6c24d3fd JG |
4048 | pthread_mutex_unlock(&client->lock); |
4049 | ||
f2b3ef9f JG |
4050 | ret = client_handle_transmission_status( |
4051 | client, transmission_status, state); | |
ab0ee2ca JG |
4052 | if (ret) { |
4053 | goto end; | |
4054 | } | |
4055 | end: | |
98b5ff34 | 4056 | rcu_read_unlock(); |
ab0ee2ca JG |
4057 | return ret; |
4058 | } | |
4059 | ||
4060 | static | |
e8360425 JD |
4061 | bool evaluate_buffer_usage_condition(const struct lttng_condition *condition, |
4062 | const struct channel_state_sample *sample, | |
4063 | uint64_t buffer_capacity) | |
ab0ee2ca JG |
4064 | { |
4065 | bool result = false; | |
4066 | uint64_t threshold; | |
4067 | enum lttng_condition_type condition_type; | |
e8360425 | 4068 | const struct lttng_condition_buffer_usage *use_condition = container_of( |
ab0ee2ca JG |
4069 | condition, struct lttng_condition_buffer_usage, |
4070 | parent); | |
4071 | ||
ab0ee2ca JG |
4072 | if (use_condition->threshold_bytes.set) { |
4073 | threshold = use_condition->threshold_bytes.value; | |
4074 | } else { | |
4075 | /* | |
4076 | * Threshold was expressed as a ratio. | |
4077 | * | |
4078 | * TODO the threshold (in bytes) of conditions expressed | |
4079 | * as a ratio of total buffer size could be cached to | |
4080 | * forego this double-multiplication or it could be performed | |
4081 | * as fixed-point math. | |
4082 | * | |
d49487dc | 4083 | * Note that caching should accommodates the case where the |
ab0ee2ca JG |
4084 | * condition applies to multiple channels (i.e. don't assume |
4085 | * that all channels matching my_chann* have the same size...) | |
4086 | */ | |
4087 | threshold = (uint64_t) (use_condition->threshold_ratio.value * | |
4088 | (double) buffer_capacity); | |
4089 | } | |
4090 | ||
4091 | condition_type = lttng_condition_get_type(condition); | |
4092 | if (condition_type == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW) { | |
4093 | DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
4094 | threshold, sample->highest_usage); | |
4095 | ||
4096 | /* | |
4097 | * The low condition should only be triggered once _all_ of the | |
4098 | * streams in a channel have gone below the "low" threshold. | |
4099 | */ | |
4100 | if (sample->highest_usage <= threshold) { | |
4101 | result = true; | |
4102 | } | |
4103 | } else { | |
4104 | DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64 ", highest usage = %" PRIu64, | |
4105 | threshold, sample->highest_usage); | |
4106 | ||
4107 | /* | |
4108 | * For high buffer usage scenarios, we want to trigger whenever | |
4109 | * _any_ of the streams has reached the "high" threshold. | |
4110 | */ | |
4111 | if (sample->highest_usage >= threshold) { | |
4112 | result = true; | |
4113 | } | |
4114 | } | |
e8360425 | 4115 | |
ab0ee2ca JG |
4116 | return result; |
4117 | } | |
4118 | ||
4119 | static | |
e8360425 JD |
4120 | bool evaluate_session_consumed_size_condition( |
4121 | const struct lttng_condition *condition, | |
4122 | uint64_t session_consumed_size) | |
4123 | { | |
4124 | uint64_t threshold; | |
4125 | const struct lttng_condition_session_consumed_size *size_condition = | |
4126 | container_of(condition, | |
4127 | struct lttng_condition_session_consumed_size, | |
4128 | parent); | |
4129 | ||
4130 | threshold = size_condition->consumed_threshold_bytes.value; | |
4131 | DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64 ", current size = %" PRIu64, | |
4132 | threshold, session_consumed_size); | |
4133 | return session_consumed_size >= threshold; | |
4134 | } | |
4135 | ||
4136 | static | |
ed327204 | 4137 | int evaluate_buffer_condition(const struct lttng_condition *condition, |
ab0ee2ca | 4138 | struct lttng_evaluation **evaluation, |
e8360425 JD |
4139 | const struct notification_thread_state *state, |
4140 | const struct channel_state_sample *previous_sample, | |
4141 | const struct channel_state_sample *latest_sample, | |
4142 | uint64_t previous_session_consumed_total, | |
4143 | uint64_t latest_session_consumed_total, | |
4144 | struct channel_info *channel_info) | |
ab0ee2ca JG |
4145 | { |
4146 | int ret = 0; | |
4147 | enum lttng_condition_type condition_type; | |
e8360425 JD |
4148 | const bool previous_sample_available = !!previous_sample; |
4149 | bool previous_sample_result = false; | |
ab0ee2ca JG |
4150 | bool latest_sample_result; |
4151 | ||
4152 | condition_type = lttng_condition_get_type(condition); | |
ab0ee2ca | 4153 | |
e8360425 JD |
4154 | switch (condition_type) { |
4155 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
4156 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
4157 | if (caa_likely(previous_sample_available)) { | |
4158 | previous_sample_result = | |
4159 | evaluate_buffer_usage_condition(condition, | |
4160 | previous_sample, channel_info->capacity); | |
4161 | } | |
4162 | latest_sample_result = evaluate_buffer_usage_condition( | |
4163 | condition, latest_sample, | |
4164 | channel_info->capacity); | |
4165 | break; | |
4166 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
4167 | if (caa_likely(previous_sample_available)) { | |
4168 | previous_sample_result = | |
4169 | evaluate_session_consumed_size_condition( | |
4170 | condition, | |
4171 | previous_session_consumed_total); | |
4172 | } | |
4173 | latest_sample_result = | |
4174 | evaluate_session_consumed_size_condition( | |
4175 | condition, | |
4176 | latest_session_consumed_total); | |
4177 | break; | |
4178 | default: | |
4179 | /* Unknown condition type; internal error. */ | |
4180 | abort(); | |
4181 | } | |
ab0ee2ca JG |
4182 | |
4183 | if (!latest_sample_result || | |
4184 | (previous_sample_result == latest_sample_result)) { | |
4185 | /* | |
4186 | * Only trigger on a condition evaluation transition. | |
4187 | * | |
4188 | * NOTE: This edge-triggered logic may not be appropriate for | |
4189 | * future condition types. | |
4190 | */ | |
4191 | goto end; | |
4192 | } | |
4193 | ||
e8360425 JD |
4194 | if (!evaluation || !latest_sample_result) { |
4195 | goto end; | |
4196 | } | |
4197 | ||
4198 | switch (condition_type) { | |
4199 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
4200 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
ab0ee2ca JG |
4201 | *evaluation = lttng_evaluation_buffer_usage_create( |
4202 | condition_type, | |
4203 | latest_sample->highest_usage, | |
e8360425 JD |
4204 | channel_info->capacity); |
4205 | break; | |
4206 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: | |
4207 | *evaluation = lttng_evaluation_session_consumed_size_create( | |
e8360425 JD |
4208 | latest_session_consumed_total); |
4209 | break; | |
4210 | default: | |
4211 | abort(); | |
4212 | } | |
4213 | ||
4214 | if (!*evaluation) { | |
4215 | ret = -1; | |
4216 | goto end; | |
ab0ee2ca JG |
4217 | } |
4218 | end: | |
4219 | return ret; | |
4220 | } | |
4221 | ||
4222 | static | |
f2b3ef9f | 4223 | int client_notification_overflow(struct notification_client *client) |
ab0ee2ca | 4224 | { |
f2b3ef9f JG |
4225 | int ret = 0; |
4226 | const struct lttng_notification_channel_message msg = { | |
ab0ee2ca | 4227 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED, |
ab0ee2ca JG |
4228 | }; |
4229 | ||
505b2d90 JG |
4230 | ASSERT_LOCKED(client->lock); |
4231 | ||
f2b3ef9f JG |
4232 | DBG("Dropping notification addressed to client (socket fd = %i)", |
4233 | client->socket); | |
4234 | if (client->communication.outbound.dropped_notification) { | |
4235 | /* | |
4236 | * The client already has a "notification dropped" message | |
4237 | * in its outgoing queue. Nothing to do since all | |
4238 | * of those messages are coalesced. | |
4239 | */ | |
4240 | goto end; | |
4241 | } | |
4242 | ||
4243 | client->communication.outbound.dropped_notification = true; | |
ab0ee2ca | 4244 | ret = lttng_dynamic_buffer_append( |
882093ee | 4245 | &client->communication.outbound.payload.buffer, &msg, |
ab0ee2ca | 4246 | sizeof(msg)); |
f2b3ef9f JG |
4247 | if (ret) { |
4248 | PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue", | |
4249 | client->socket); | |
4250 | } | |
4251 | end: | |
ab0ee2ca JG |
4252 | return ret; |
4253 | } | |
4254 | ||
f2b3ef9f JG |
4255 | static int client_handle_transmission_status_wrapper( |
4256 | struct notification_client *client, | |
4257 | enum client_transmission_status status, | |
4258 | void *user_data) | |
4259 | { | |
4260 | return client_handle_transmission_status(client, status, | |
4261 | (struct notification_thread_state *) user_data); | |
4262 | } | |
4263 | ||
4264 | static | |
4265 | int send_evaluation_to_clients(const struct lttng_trigger *trigger, | |
4266 | const struct lttng_evaluation *evaluation, | |
4267 | struct notification_client_list* client_list, | |
4268 | struct notification_thread_state *state, | |
4269 | uid_t object_uid, gid_t object_gid) | |
4270 | { | |
ff588497 JR |
4271 | const struct lttng_credentials creds = { |
4272 | .uid = LTTNG_OPTIONAL_INIT_VALUE(object_uid), | |
4273 | .gid = LTTNG_OPTIONAL_INIT_VALUE(object_gid), | |
4274 | }; | |
4275 | ||
f2b3ef9f | 4276 | return notification_client_list_send_evaluation(client_list, |
52d55cf9 | 4277 | trigger, evaluation, |
ff588497 | 4278 | &creds, |
f2b3ef9f JG |
4279 | client_handle_transmission_status_wrapper, state); |
4280 | } | |
4281 | ||
f37d0f86 JG |
4282 | /* |
4283 | * Permission checks relative to notification channel clients are performed | |
4284 | * here. Notice how object, client, and trigger credentials are involved in | |
4285 | * this check. | |
4286 | * | |
4287 | * The `object` credentials are the credentials associated with the "subject" | |
4288 | * of a condition. For instance, a `rotation completed` condition applies | |
4289 | * to a session. When that condition is met, it will produce an evaluation | |
4290 | * against a session. Hence, in this case, the `object` credentials are the | |
4291 | * credentials of the "subject" session. | |
4292 | * | |
4293 | * The `trigger` credentials are the credentials of the user that registered the | |
4294 | * trigger. | |
4295 | * | |
4296 | * The `client` credentials are the credentials of the user that created a given | |
4297 | * notification channel. | |
4298 | * | |
4299 | * In terms of visibility, it is expected that non-privilieged users can only | |
4300 | * register triggers against "their" objects (their own sessions and | |
4301 | * applications they are allowed to interact with). They can then open a | |
4302 | * notification channel and subscribe to notifications associated with those | |
4303 | * triggers. | |
4304 | * | |
4305 | * As for privilieged users, they can register triggers against the objects of | |
4306 | * other users. They can then subscribe to the notifications associated to their | |
4307 | * triggers. Privilieged users _can't_ subscribe to the notifications of | |
4308 | * triggers owned by other users; they must create their own triggers. | |
4309 | * | |
4310 | * This is more a concern of usability than security. It would be difficult for | |
4311 | * a root user reliably subscribe to a specific set of conditions without | |
4312 | * interference from external users (those could, for instance, unregister | |
4313 | * their triggers). | |
4314 | */ | |
f2b3ef9f JG |
4315 | LTTNG_HIDDEN |
4316 | int notification_client_list_send_evaluation( | |
4317 | struct notification_client_list *client_list, | |
52d55cf9 | 4318 | const struct lttng_trigger *trigger, |
9b63a4aa | 4319 | const struct lttng_evaluation *evaluation, |
f2b3ef9f JG |
4320 | const struct lttng_credentials *source_object_creds, |
4321 | report_client_transmission_result_cb client_report, | |
4322 | void *user_data) | |
ab0ee2ca JG |
4323 | { |
4324 | int ret = 0; | |
c0a66c84 | 4325 | struct lttng_payload msg_payload; |
ab0ee2ca | 4326 | struct notification_client_list_element *client_list_element, *tmp; |
9b63a4aa | 4327 | const struct lttng_notification notification = { |
52d55cf9 | 4328 | .trigger = (struct lttng_trigger *) trigger, |
9b63a4aa JG |
4329 | .evaluation = (struct lttng_evaluation *) evaluation, |
4330 | }; | |
3647288f JG |
4331 | struct lttng_notification_channel_message msg_header = { |
4332 | .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION, | |
4333 | }; | |
52d55cf9 JG |
4334 | const struct lttng_credentials *trigger_creds = |
4335 | lttng_trigger_get_credentials(trigger); | |
ab0ee2ca | 4336 | |
c0a66c84 | 4337 | lttng_payload_init(&msg_payload); |
ab0ee2ca | 4338 | |
c0a66c84 | 4339 | ret = lttng_dynamic_buffer_append(&msg_payload.buffer, &msg_header, |
3647288f | 4340 | sizeof(msg_header)); |
ab0ee2ca JG |
4341 | if (ret) { |
4342 | goto end; | |
4343 | } | |
4344 | ||
c0a66c84 | 4345 | ret = lttng_notification_serialize(¬ification, &msg_payload); |
ab0ee2ca | 4346 | if (ret) { |
ab0ee2ca JG |
4347 | ERR("[notification-thread] Failed to serialize notification"); |
4348 | ret = -1; | |
4349 | goto end; | |
4350 | } | |
4351 | ||
3647288f | 4352 | /* Update payload size. */ |
505b2d90 JG |
4353 | ((struct lttng_notification_channel_message *) msg_payload.buffer.data) |
4354 | ->size = (uint32_t)( | |
4355 | msg_payload.buffer.size - sizeof(msg_header)); | |
3647288f | 4356 | |
882093ee JR |
4357 | /* Update the payload number of fds. */ |
4358 | { | |
4359 | const struct lttng_payload_view pv = lttng_payload_view_from_payload( | |
4360 | &msg_payload, 0, -1); | |
4361 | ||
4362 | ((struct lttng_notification_channel_message *) | |
4363 | msg_payload.buffer.data)->fds = (uint32_t) | |
4364 | lttng_payload_view_get_fd_handle_count(&pv); | |
4365 | } | |
4366 | ||
505b2d90 | 4367 | pthread_mutex_lock(&client_list->lock); |
ab0ee2ca | 4368 | cds_list_for_each_entry_safe(client_list_element, tmp, |
091fa780 | 4369 | &client_list->clients_list, node) { |
f2b3ef9f | 4370 | enum client_transmission_status transmission_status; |
ab0ee2ca JG |
4371 | struct notification_client *client = |
4372 | client_list_element->client; | |
4373 | ||
505b2d90 JG |
4374 | ret = 0; |
4375 | pthread_mutex_lock(&client->lock); | |
6c24d3fd JG |
4376 | if (!client->communication.active) { |
4377 | /* | |
4378 | * Skip inactive client (protocol error or | |
4379 | * disconnecting). | |
4380 | */ | |
4381 | DBG("Skipping client at it is marked as inactive"); | |
4382 | goto skip_client; | |
4383 | } | |
4384 | ||
f2b3ef9f | 4385 | if (source_object_creds) { |
ff588497 JR |
4386 | if (client->uid != lttng_credentials_get_uid(source_object_creds) && |
4387 | client->gid != lttng_credentials_get_gid(source_object_creds) && | |
f2b3ef9f JG |
4388 | client->uid != 0) { |
4389 | /* | |
4390 | * Client is not allowed to monitor this | |
4391 | * object. | |
4392 | */ | |
4393 | DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger"); | |
6c24d3fd | 4394 | goto skip_client; |
f2b3ef9f | 4395 | } |
90843f49 JR |
4396 | } |
4397 | ||
f712e5f6 | 4398 | if (client->uid != lttng_credentials_get_uid(trigger_creds)) { |
90843f49 | 4399 | DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger"); |
6c24d3fd | 4400 | goto skip_client; |
ab0ee2ca JG |
4401 | } |
4402 | ||
4403 | DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)", | |
c0a66c84 | 4404 | client->socket, msg_payload.buffer.size); |
882093ee JR |
4405 | |
4406 | if (client_has_outbound_data_left(client)) { | |
ab0ee2ca JG |
4407 | /* |
4408 | * Outgoing data is already buffered for this client; | |
4409 | * drop the notification and enqueue a "dropped | |
4410 | * notification" message if this is the first dropped | |
4411 | * notification since the socket spilled-over to the | |
4412 | * queue. | |
4413 | */ | |
f2b3ef9f JG |
4414 | ret = client_notification_overflow(client); |
4415 | if (ret) { | |
6c24d3fd JG |
4416 | /* Fatal error. */ |
4417 | goto skip_client; | |
ab0ee2ca | 4418 | } |
ab0ee2ca JG |
4419 | } |
4420 | ||
882093ee | 4421 | ret = lttng_payload_copy(&msg_payload, &client->communication.outbound.payload); |
ab0ee2ca | 4422 | if (ret) { |
6c24d3fd JG |
4423 | /* Fatal error. */ |
4424 | goto skip_client; | |
ab0ee2ca JG |
4425 | } |
4426 | ||
f2b3ef9f | 4427 | transmission_status = client_flush_outgoing_queue(client); |
6c24d3fd | 4428 | pthread_mutex_unlock(&client->lock); |
f2b3ef9f | 4429 | ret = client_report(client, transmission_status, user_data); |
ab0ee2ca | 4430 | if (ret) { |
6c24d3fd JG |
4431 | /* Fatal error. */ |
4432 | goto end_unlock_list; | |
505b2d90 | 4433 | } |
6c24d3fd JG |
4434 | |
4435 | continue; | |
4436 | ||
4437 | skip_client: | |
505b2d90 JG |
4438 | pthread_mutex_unlock(&client->lock); |
4439 | if (ret) { | |
6c24d3fd | 4440 | /* Fatal error. */ |
505b2d90 | 4441 | goto end_unlock_list; |
ab0ee2ca JG |
4442 | } |
4443 | } | |
4444 | ret = 0; | |
505b2d90 JG |
4445 | |
4446 | end_unlock_list: | |
4447 | pthread_mutex_unlock(&client_list->lock); | |
ab0ee2ca | 4448 | end: |
c0a66c84 | 4449 | lttng_payload_reset(&msg_payload); |
ab0ee2ca JG |
4450 | return ret; |
4451 | } | |
4452 | ||
8b524060 FD |
4453 | static |
4454 | struct lttng_event_notifier_notification *recv_one_event_notifier_notification( | |
b9a8d78f | 4455 | int notification_pipe_read_fd, enum lttng_domain_type domain) |
94078603 JR |
4456 | { |
4457 | int ret; | |
b9a8d78f FD |
4458 | uint64_t token; |
4459 | struct lttng_event_notifier_notification *notification = NULL; | |
82b3cbf4 JR |
4460 | char *capture_buffer = NULL; |
4461 | size_t capture_buffer_size; | |
94078603 JR |
4462 | void *reception_buffer; |
4463 | size_t reception_size; | |
4464 | ||
fc4b93fa | 4465 | struct lttng_ust_abi_event_notifier_notification ust_notification; |
b9a8d78f | 4466 | struct lttng_kernel_event_notifier_notification kernel_notification; |
94078603 | 4467 | |
b9a8d78f | 4468 | /* Init lttng_event_notifier_notification */ |
94078603 JR |
4469 | switch(domain) { |
4470 | case LTTNG_DOMAIN_UST: | |
4471 | reception_buffer = (void *) &ust_notification; | |
4472 | reception_size = sizeof(ust_notification); | |
94078603 JR |
4473 | break; |
4474 | case LTTNG_DOMAIN_KERNEL: | |
4475 | reception_buffer = (void *) &kernel_notification; | |
4476 | reception_size = sizeof(kernel_notification); | |
94078603 JR |
4477 | break; |
4478 | default: | |
4479 | abort(); | |
4480 | } | |
4481 | ||
4482 | /* | |
4483 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
4484 | * ensuring that read/write of tracer notifications are atomic. | |
4485 | */ | |
4486 | ret = lttng_read(notification_pipe_read_fd, reception_buffer, | |
4487 | reception_size); | |
4488 | if (ret != reception_size) { | |
4489 | PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d", | |
4490 | notification_pipe_read_fd, reception_size, ret); | |
4491 | ret = -1; | |
4492 | goto end; | |
4493 | } | |
4494 | ||
4495 | switch(domain) { | |
4496 | case LTTNG_DOMAIN_UST: | |
b9a8d78f | 4497 | token = ust_notification.token; |
82b3cbf4 | 4498 | capture_buffer_size = ust_notification.capture_buf_size; |
94078603 JR |
4499 | break; |
4500 | case LTTNG_DOMAIN_KERNEL: | |
b9a8d78f | 4501 | token = kernel_notification.token; |
bbd6675c | 4502 | capture_buffer_size = kernel_notification.capture_buf_size; |
94078603 JR |
4503 | break; |
4504 | default: | |
4505 | abort(); | |
4506 | } | |
4507 | ||
82b3cbf4 JR |
4508 | if (capture_buffer_size == 0) { |
4509 | capture_buffer = NULL; | |
4510 | goto skip_capture; | |
4511 | } | |
4512 | ||
4513 | if (capture_buffer_size > MAX_CAPTURE_SIZE) { | |
4514 | ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes", | |
4515 | capture_buffer_size, MAX_CAPTURE_SIZE); | |
4516 | goto end; | |
4517 | } | |
4518 | ||
4519 | capture_buffer = zmalloc(capture_buffer_size); | |
4520 | if (!capture_buffer) { | |
4521 | ERR("[notification-thread] Failed to allocate capture buffer"); | |
4522 | goto end; | |
4523 | } | |
4524 | ||
4525 | /* Fetch additional payload (capture). */ | |
4526 | ret = lttng_read(notification_pipe_read_fd, capture_buffer, capture_buffer_size); | |
4527 | if (ret != capture_buffer_size) { | |
4528 | ERR("[notification-thread] Failed to read from event source pipe (fd = %i)", | |
4529 | notification_pipe_read_fd); | |
4530 | goto end; | |
4531 | } | |
4532 | ||
4533 | skip_capture: | |
4534 | notification = lttng_event_notifier_notification_create(token, domain, | |
4535 | capture_buffer, capture_buffer_size); | |
4536 | if (notification == NULL) { | |
4537 | goto end; | |
4538 | } | |
4539 | ||
4540 | /* | |
4541 | * Ownership transfered to the lttng_event_notifier_notification object. | |
4542 | */ | |
4543 | capture_buffer = NULL; | |
4544 | ||
b9a8d78f | 4545 | end: |
82b3cbf4 | 4546 | free(capture_buffer); |
b9a8d78f FD |
4547 | return notification; |
4548 | } | |
4549 | ||
8b524060 FD |
4550 | static |
4551 | int dispatch_one_event_notifier_notification(struct notification_thread_state *state, | |
4552 | struct lttng_event_notifier_notification *notification) | |
b9a8d78f | 4553 | { |
b9a8d78f FD |
4554 | struct cds_lfht_node *node; |
4555 | struct cds_lfht_iter iter; | |
4556 | struct notification_trigger_tokens_ht_element *element; | |
4557 | struct lttng_evaluation *evaluation = NULL; | |
b9a8d78f FD |
4558 | enum action_executor_status executor_status; |
4559 | struct notification_client_list *client_list = NULL; | |
8b524060 | 4560 | int ret; |
7c920b63 | 4561 | unsigned int capture_count = 0; |
b9a8d78f | 4562 | |
94078603 JR |
4563 | /* Find triggers associated with this token. */ |
4564 | rcu_read_lock(); | |
4565 | cds_lfht_lookup(state->trigger_tokens_ht, | |
b9a8d78f FD |
4566 | hash_key_u64(¬ification->tracer_token, lttng_ht_seed), |
4567 | match_trigger_token, ¬ification->tracer_token, &iter); | |
94078603 | 4568 | node = cds_lfht_iter_get_node(&iter); |
f1446131 | 4569 | if (caa_unlikely(!node)) { |
94078603 | 4570 | /* |
f1446131 JR |
4571 | * This is not an error, slow consumption of the tracer |
4572 | * notifications can lead to situations where a trigger is | |
4573 | * removed but we still get tracer notifications matching a | |
4574 | * trigger that no longer exists. | |
94078603 JR |
4575 | */ |
4576 | ret = 0; | |
4577 | goto end_unlock; | |
4578 | } | |
4579 | ||
4580 | element = caa_container_of(node, | |
4581 | struct notification_trigger_tokens_ht_element, | |
4582 | node); | |
4583 | ||
8dbb86b8 | 4584 | if (lttng_condition_event_rule_matches_get_capture_descriptor_count( |
7c920b63 PP |
4585 | lttng_trigger_get_const_condition(element->trigger), |
4586 | &capture_count) != LTTNG_CONDITION_STATUS_OK) { | |
4587 | ERR("Failed to get capture count"); | |
4588 | ret = -1; | |
4589 | goto end; | |
4590 | } | |
4591 | ||
4592 | if (!notification->capture_buffer && capture_count != 0) { | |
4593 | ERR("Expected capture but capture buffer is null"); | |
4594 | ret = -1; | |
4595 | goto end; | |
4596 | } | |
4597 | ||
8dbb86b8 | 4598 | evaluation = lttng_evaluation_event_rule_matches_create( |
7c920b63 PP |
4599 | container_of(lttng_trigger_get_const_condition( |
4600 | element->trigger), | |
8dbb86b8 | 4601 | struct lttng_condition_event_rule_matches, |
7c920b63 | 4602 | parent), |
7c920b63 PP |
4603 | notification->capture_buffer, |
4604 | notification->capture_buf_size, false); | |
4605 | ||
f1446131 | 4606 | if (evaluation == NULL) { |
b9a8d78f | 4607 | ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job"); |
f1446131 JR |
4608 | ret = -1; |
4609 | goto end_unlock; | |
4610 | } | |
f1446131 JR |
4611 | client_list = get_client_list_from_condition(state, |
4612 | lttng_trigger_get_const_condition(element->trigger)); | |
72365501 | 4613 | executor_status = action_executor_enqueue_trigger(state->executor, |
f1446131 JR |
4614 | element->trigger, evaluation, NULL, client_list); |
4615 | switch (executor_status) { | |
4616 | case ACTION_EXECUTOR_STATUS_OK: | |
4617 | ret = 0; | |
4618 | break; | |
4619 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
4620 | { | |
4621 | struct notification_client_list_element *client_list_element, | |
4622 | *tmp; | |
4623 | ||
4624 | /* | |
4625 | * Not a fatal error; this is expected and simply means the | |
4626 | * executor has too much work queued already. | |
4627 | */ | |
4628 | ret = 0; | |
4629 | ||
4630 | /* No clients subscribed to notifications for this trigger. */ | |
4631 | if (!client_list) { | |
4632 | break; | |
4633 | } | |
4634 | ||
4635 | /* Warn clients that a notification (or more) was dropped. */ | |
4636 | pthread_mutex_lock(&client_list->lock); | |
4637 | cds_list_for_each_entry_safe(client_list_element, tmp, | |
091fa780 | 4638 | &client_list->clients_list, node) { |
f1446131 JR |
4639 | enum client_transmission_status transmission_status; |
4640 | struct notification_client *client = | |
4641 | client_list_element->client; | |
4642 | ||
4643 | pthread_mutex_lock(&client->lock); | |
4644 | ret = client_notification_overflow(client); | |
4645 | if (ret) { | |
4646 | /* Fatal error. */ | |
4647 | goto next_client; | |
4648 | } | |
4649 | ||
4650 | transmission_status = | |
4651 | client_flush_outgoing_queue(client); | |
4652 | ret = client_handle_transmission_status( | |
4653 | client, transmission_status, state); | |
4654 | if (ret) { | |
4655 | /* Fatal error. */ | |
4656 | goto next_client; | |
4657 | } | |
4658 | next_client: | |
4659 | pthread_mutex_unlock(&client->lock); | |
4660 | if (ret) { | |
4661 | break; | |
4662 | } | |
4663 | } | |
4664 | ||
4665 | pthread_mutex_unlock(&client_list->lock); | |
4666 | break; | |
4667 | } | |
98dfca79 | 4668 | case ACTION_EXECUTOR_STATUS_INVALID: |
f1446131 JR |
4669 | case ACTION_EXECUTOR_STATUS_ERROR: |
4670 | /* Fatal error, shut down everything. */ | |
4671 | ERR("Fatal error encoutered while enqueuing action to the action executor"); | |
4672 | ret = -1; | |
4673 | goto end_unlock; | |
4674 | default: | |
4675 | /* Unhandled error. */ | |
4676 | abort(); | |
4677 | } | |
94078603 JR |
4678 | |
4679 | end_unlock: | |
f1446131 | 4680 | notification_client_list_put(client_list); |
94078603 | 4681 | rcu_read_unlock(); |
7c920b63 | 4682 | end: |
8b524060 FD |
4683 | return ret; |
4684 | } | |
4685 | ||
4686 | static | |
4687 | int handle_one_event_notifier_notification( | |
4688 | struct notification_thread_state *state, | |
4689 | int pipe, enum lttng_domain_type domain) | |
4690 | { | |
82b3cbf4 | 4691 | int ret = 0; |
8b524060 FD |
4692 | struct lttng_event_notifier_notification *notification = NULL; |
4693 | ||
4694 | notification = recv_one_event_notifier_notification(pipe, domain); | |
4695 | if (notification == NULL) { | |
82b3cbf4 | 4696 | /* Reception failed, don't consider it fatal. */ |
8b524060 FD |
4697 | ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s", |
4698 | pipe, lttng_domain_type_str(domain)); | |
8b524060 FD |
4699 | goto end; |
4700 | } | |
4701 | ||
4702 | ret = dispatch_one_event_notifier_notification(state, notification); | |
4703 | if (ret) { | |
4704 | ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s", | |
4705 | pipe, lttng_domain_type_str(domain)); | |
4706 | goto end; | |
4707 | } | |
4708 | ||
94078603 | 4709 | end: |
8b524060 | 4710 | lttng_event_notifier_notification_destroy(notification); |
94078603 JR |
4711 | return ret; |
4712 | } | |
4713 | ||
8b524060 FD |
4714 | int handle_notification_thread_event_notification(struct notification_thread_state *state, |
4715 | int pipe, enum lttng_domain_type domain) | |
4716 | { | |
4717 | return handle_one_event_notifier_notification(state, pipe, domain); | |
4718 | } | |
4719 | ||
ab0ee2ca JG |
4720 | int handle_notification_thread_channel_sample( |
4721 | struct notification_thread_state *state, int pipe, | |
4722 | enum lttng_domain_type domain) | |
4723 | { | |
4724 | int ret = 0; | |
4725 | struct lttcomm_consumer_channel_monitor_msg sample_msg; | |
ab0ee2ca JG |
4726 | struct channel_info *channel_info; |
4727 | struct cds_lfht_node *node; | |
4728 | struct cds_lfht_iter iter; | |
4729 | struct lttng_channel_trigger_list *trigger_list; | |
4730 | struct lttng_trigger_list_element *trigger_list_element; | |
4731 | bool previous_sample_available = false; | |
e8360425 JD |
4732 | struct channel_state_sample previous_sample, latest_sample; |
4733 | uint64_t previous_session_consumed_total, latest_session_consumed_total; | |
f2b3ef9f | 4734 | struct lttng_credentials channel_creds; |
ab0ee2ca JG |
4735 | |
4736 | /* | |
4737 | * The monitoring pipe only holds messages smaller than PIPE_BUF, | |
4738 | * ensuring that read/write of sampling messages are atomic. | |
4739 | */ | |
7c2551ef | 4740 | ret = lttng_read(pipe, &sample_msg, sizeof(sample_msg)); |
ab0ee2ca JG |
4741 | if (ret != sizeof(sample_msg)) { |
4742 | ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)", | |
4743 | pipe); | |
4744 | ret = -1; | |
4745 | goto end; | |
4746 | } | |
4747 | ||
4748 | ret = 0; | |
4749 | latest_sample.key.key = sample_msg.key; | |
4750 | latest_sample.key.domain = domain; | |
4751 | latest_sample.highest_usage = sample_msg.highest; | |
4752 | latest_sample.lowest_usage = sample_msg.lowest; | |
e8360425 | 4753 | latest_sample.channel_total_consumed = sample_msg.total_consumed; |
ab0ee2ca JG |
4754 | |
4755 | rcu_read_lock(); | |
4756 | ||
4757 | /* Retrieve the channel's informations */ | |
4758 | cds_lfht_lookup(state->channels_ht, | |
4759 | hash_channel_key(&latest_sample.key), | |
4760 | match_channel_info, | |
4761 | &latest_sample.key, | |
4762 | &iter); | |
4763 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4764 | if (caa_unlikely(!node)) { |
ab0ee2ca JG |
4765 | /* |
4766 | * Not an error since the consumer can push a sample to the pipe | |
4767 | * and the rest of the session daemon could notify us of the | |
4768 | * channel's destruction before we get a chance to process that | |
4769 | * sample. | |
4770 | */ | |
4771 | DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64 " in %s domain", | |
4772 | latest_sample.key.key, | |
526200e4 | 4773 | lttng_domain_type_str(domain)); |
ab0ee2ca JG |
4774 | goto end_unlock; |
4775 | } | |
4776 | channel_info = caa_container_of(node, struct channel_info, | |
4777 | channels_ht_node); | |
e8360425 | 4778 | DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64 ") in session %s (highest usage = %" PRIu64 ", lowest usage = %" PRIu64", total consumed = %" PRIu64")", |
8abe313a | 4779 | channel_info->name, |
ab0ee2ca | 4780 | latest_sample.key.key, |
8abe313a | 4781 | channel_info->session_info->name, |
ab0ee2ca | 4782 | latest_sample.highest_usage, |
e8360425 JD |
4783 | latest_sample.lowest_usage, |
4784 | latest_sample.channel_total_consumed); | |
4785 | ||
4786 | previous_session_consumed_total = | |
4787 | channel_info->session_info->consumed_data_size; | |
ab0ee2ca JG |
4788 | |
4789 | /* Retrieve the channel's last sample, if it exists, and update it. */ | |
4790 | cds_lfht_lookup(state->channel_state_ht, | |
4791 | hash_channel_key(&latest_sample.key), | |
4792 | match_channel_state_sample, | |
4793 | &latest_sample.key, | |
4794 | &iter); | |
4795 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4796 | if (caa_likely(node)) { |
ab0ee2ca JG |
4797 | struct channel_state_sample *stored_sample; |
4798 | ||
4799 | /* Update the sample stored. */ | |
4800 | stored_sample = caa_container_of(node, | |
4801 | struct channel_state_sample, | |
4802 | channel_state_ht_node); | |
e8360425 | 4803 | |
ab0ee2ca JG |
4804 | memcpy(&previous_sample, stored_sample, |
4805 | sizeof(previous_sample)); | |
4806 | stored_sample->highest_usage = latest_sample.highest_usage; | |
4807 | stored_sample->lowest_usage = latest_sample.lowest_usage; | |
0f0479d7 | 4808 | stored_sample->channel_total_consumed = latest_sample.channel_total_consumed; |
ab0ee2ca | 4809 | previous_sample_available = true; |
e8360425 JD |
4810 | |
4811 | latest_session_consumed_total = | |
4812 | previous_session_consumed_total + | |
4813 | (latest_sample.channel_total_consumed - previous_sample.channel_total_consumed); | |
ab0ee2ca JG |
4814 | } else { |
4815 | /* | |
4816 | * This is the channel's first sample, allocate space for and | |
4817 | * store the new sample. | |
4818 | */ | |
4819 | struct channel_state_sample *stored_sample; | |
4820 | ||
4821 | stored_sample = zmalloc(sizeof(*stored_sample)); | |
4822 | if (!stored_sample) { | |
4823 | ret = -1; | |
4824 | goto end_unlock; | |
4825 | } | |
4826 | ||
4827 | memcpy(stored_sample, &latest_sample, sizeof(*stored_sample)); | |
4828 | cds_lfht_node_init(&stored_sample->channel_state_ht_node); | |
4829 | cds_lfht_add(state->channel_state_ht, | |
4830 | hash_channel_key(&stored_sample->key), | |
4831 | &stored_sample->channel_state_ht_node); | |
e8360425 JD |
4832 | |
4833 | latest_session_consumed_total = | |
4834 | previous_session_consumed_total + | |
4835 | latest_sample.channel_total_consumed; | |
ab0ee2ca JG |
4836 | } |
4837 | ||
e8360425 JD |
4838 | channel_info->session_info->consumed_data_size = |
4839 | latest_session_consumed_total; | |
4840 | ||
ab0ee2ca JG |
4841 | /* Find triggers associated with this channel. */ |
4842 | cds_lfht_lookup(state->channel_triggers_ht, | |
4843 | hash_channel_key(&latest_sample.key), | |
4844 | match_channel_trigger_list, | |
4845 | &latest_sample.key, | |
4846 | &iter); | |
4847 | node = cds_lfht_iter_get_node(&iter); | |
e0b5f87b | 4848 | if (caa_likely(!node)) { |
ab0ee2ca JG |
4849 | goto end_unlock; |
4850 | } | |
4851 | ||
f2b3ef9f | 4852 | channel_creds = (typeof(channel_creds)) { |
ff588497 JR |
4853 | .uid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->uid), |
4854 | .gid = LTTNG_OPTIONAL_INIT_VALUE(channel_info->session_info->gid), | |
f2b3ef9f JG |
4855 | }; |
4856 | ||
ab0ee2ca JG |
4857 | trigger_list = caa_container_of(node, struct lttng_channel_trigger_list, |
4858 | channel_triggers_ht_node); | |
4859 | cds_list_for_each_entry(trigger_list_element, &trigger_list->list, | |
e742b055 | 4860 | node) { |
9b63a4aa | 4861 | const struct lttng_condition *condition; |
f2b3ef9f | 4862 | struct lttng_trigger *trigger; |
505b2d90 | 4863 | struct notification_client_list *client_list = NULL; |
ab0ee2ca | 4864 | struct lttng_evaluation *evaluation = NULL; |
f2b3ef9f | 4865 | enum action_executor_status executor_status; |
ab0ee2ca | 4866 | |
505b2d90 | 4867 | ret = 0; |
ab0ee2ca | 4868 | trigger = trigger_list_element->trigger; |
9b63a4aa | 4869 | condition = lttng_trigger_get_const_condition(trigger); |
ab0ee2ca | 4870 | assert(condition); |
ab0ee2ca JG |
4871 | |
4872 | /* | |
4873 | * Check if any client is subscribed to the result of this | |
4874 | * evaluation. | |
4875 | */ | |
ed327204 | 4876 | client_list = get_client_list_from_condition(state, condition); |
ab0ee2ca | 4877 | |
ed327204 | 4878 | ret = evaluate_buffer_condition(condition, &evaluation, state, |
ab0ee2ca | 4879 | previous_sample_available ? &previous_sample : NULL, |
e8360425 JD |
4880 | &latest_sample, |
4881 | previous_session_consumed_total, | |
4882 | latest_session_consumed_total, | |
4883 | channel_info); | |
4884 | if (caa_unlikely(ret)) { | |
505b2d90 | 4885 | goto put_list; |
ab0ee2ca JG |
4886 | } |
4887 | ||
e8360425 | 4888 | if (caa_likely(!evaluation)) { |
505b2d90 | 4889 | goto put_list; |
ab0ee2ca JG |
4890 | } |
4891 | ||
f2b3ef9f JG |
4892 | /* |
4893 | * Ownership of `evaluation` transferred to the action executor | |
4894 | * no matter the result. | |
4895 | */ | |
72365501 JR |
4896 | executor_status = action_executor_enqueue_trigger( |
4897 | state->executor, trigger, evaluation, | |
4898 | &channel_creds, client_list); | |
f2b3ef9f JG |
4899 | evaluation = NULL; |
4900 | switch (executor_status) { | |
4901 | case ACTION_EXECUTOR_STATUS_OK: | |
4902 | break; | |
4903 | case ACTION_EXECUTOR_STATUS_ERROR: | |
4904 | case ACTION_EXECUTOR_STATUS_INVALID: | |
4905 | /* | |
4906 | * TODO Add trigger identification (name/id) when | |
4907 | * it is added to the API. | |
4908 | */ | |
4909 | ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger"); | |
4910 | ret = -1; | |
4911 | goto put_list; | |
4912 | case ACTION_EXECUTOR_STATUS_OVERFLOW: | |
4913 | /* | |
4914 | * TODO Add trigger identification (name/id) when | |
4915 | * it is added to the API. | |
4916 | * | |
4917 | * Not a fatal error. | |
4918 | */ | |
4919 | WARN("No space left when enqueuing action associated with buffer-condition trigger"); | |
4920 | ret = 0; | |
4921 | goto put_list; | |
4922 | default: | |
4923 | abort(); | |
4924 | } | |
4925 | ||
505b2d90 JG |
4926 | put_list: |
4927 | notification_client_list_put(client_list); | |
e0b5f87b | 4928 | if (caa_unlikely(ret)) { |
505b2d90 | 4929 | break; |
ab0ee2ca JG |
4930 | } |
4931 | } | |
4932 | end_unlock: | |
4933 | rcu_read_unlock(); | |
4934 | end: | |
4935 | return ret; | |
4936 | } |