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