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