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