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