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