2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "action-executor.h"
10 #include "health-sessiond.h"
11 #include "lttng-sessiond.h"
12 #include "notification-thread-internal.h"
15 #include <common/macros.h>
16 #include <common/optional.h>
17 #include <lttng/action/action-internal.h>
18 #include <lttng/action/group.h>
19 #include <lttng/action/notify.h>
20 #include <lttng/action/rotate-session.h>
21 #include <lttng/action/snapshot-session.h>
22 #include <lttng/action/start-session.h>
23 #include <lttng/action/stop-session.h>
24 #include <lttng/condition/evaluation.h>
25 #include <lttng/lttng-error.h>
26 #include <lttng/trigger/trigger-internal.h>
30 #include <urcu/list.h>
32 #define THREAD_NAME "Action Executor"
33 #define MAX_QUEUED_WORK_COUNT 8192
35 struct action_work_item
{
37 struct lttng_trigger
*trigger
;
38 struct lttng_evaluation
*evaluation
;
39 struct notification_client_list
*client_list
;
40 LTTNG_OPTIONAL(struct lttng_credentials
) object_creds
;
41 struct cds_list_head list_node
;
44 struct action_executor
{
45 struct lttng_thread
*thread
;
46 struct notification_thread_handle
*notification_thread_handle
;
48 uint64_t pending_count
;
49 struct cds_list_head list
;
54 uint64_t next_work_item_id
;
58 * Only return non-zero on a fatal error that should shut down the action
61 typedef int (*action_executor_handler
)(struct action_executor
*executor
,
62 const struct action_work_item
*,
63 const struct lttng_action
*action
);
65 static int action_executor_notify_handler(struct action_executor
*executor
,
66 const struct action_work_item
*,
67 const struct lttng_action
*);
68 static int action_executor_start_session_handler(struct action_executor
*executor
,
69 const struct action_work_item
*,
70 const struct lttng_action
*);
71 static int action_executor_stop_session_handler(struct action_executor
*executor
,
72 const struct action_work_item
*,
73 const struct lttng_action
*);
74 static int action_executor_rotate_session_handler(struct action_executor
*executor
,
75 const struct action_work_item
*,
76 const struct lttng_action
*);
77 static int action_executor_snapshot_session_handler(struct action_executor
*executor
,
78 const struct action_work_item
*,
79 const struct lttng_action
*);
80 static int action_executor_group_handler(struct action_executor
*executor
,
81 const struct action_work_item
*,
82 const struct lttng_action
*);
83 static int action_executor_generic_handler(struct action_executor
*executor
,
84 const struct action_work_item
*,
85 const struct lttng_action
*);
87 static const action_executor_handler action_executors
[] = {
88 [LTTNG_ACTION_TYPE_NOTIFY
] = action_executor_notify_handler
,
89 [LTTNG_ACTION_TYPE_START_SESSION
] = action_executor_start_session_handler
,
90 [LTTNG_ACTION_TYPE_STOP_SESSION
] = action_executor_stop_session_handler
,
91 [LTTNG_ACTION_TYPE_ROTATE_SESSION
] = action_executor_rotate_session_handler
,
92 [LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
] = action_executor_snapshot_session_handler
,
93 [LTTNG_ACTION_TYPE_GROUP
] = action_executor_group_handler
,
96 static const char *action_type_names
[] = {
97 [LTTNG_ACTION_TYPE_NOTIFY
] = "Notify",
98 [LTTNG_ACTION_TYPE_START_SESSION
] = "Start session",
99 [LTTNG_ACTION_TYPE_STOP_SESSION
] = "Stop session",
100 [LTTNG_ACTION_TYPE_ROTATE_SESSION
] = "Rotate session",
101 [LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
] = "Snapshot session",
102 [LTTNG_ACTION_TYPE_GROUP
] = "Group",
105 static const char *get_action_name(const struct lttng_action
*action
)
107 return action_type_names
[lttng_action_get_type(action
)];
110 /* Check if this trigger allowed to interect with a given session. */
111 static bool is_trigger_allowed_for_session(const struct lttng_trigger
*trigger
,
112 struct ltt_session
*session
)
114 bool is_allowed
= false;
115 const struct lttng_credentials session_creds
= {
116 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
117 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
119 /* Can never be NULL. */
120 const struct lttng_credentials
*trigger_creds
=
121 lttng_trigger_get_credentials(trigger
);
123 is_allowed
= (lttng_credentials_is_equal_uid(trigger_creds
, &session_creds
)) ||
124 (lttng_credentials_get_uid(trigger_creds
) == 0);
126 WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld",
128 (long int) session
->uid
,
129 (long int) session
->gid
,
130 (long int) lttng_credentials_get_uid(trigger_creds
));
136 static int client_handle_transmission_status(
137 struct notification_client
*client
,
138 enum client_transmission_status status
,
142 struct action_executor
*executor
= user_data
;
143 bool update_communication
= true;
146 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
147 DBG("Successfully sent full notification to client, client_id = %" PRIu64
,
149 update_communication
= false;
151 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
152 DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64
,
155 case CLIENT_TRANSMISSION_STATUS_FAIL
:
156 DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64
,
160 ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64
,
166 if (!update_communication
) {
170 /* Safe to read client's id without locking as it is immutable. */
171 ret
= notification_thread_client_communication_update(
172 executor
->notification_thread_handle
, client
->id
,
178 static int action_executor_notify_handler(struct action_executor
*executor
,
179 const struct action_work_item
*work_item
,
180 const struct lttng_action
*action
)
182 return notification_client_list_send_evaluation(work_item
->client_list
,
183 lttng_trigger_get_const_condition(work_item
->trigger
),
184 work_item
->evaluation
,
185 lttng_trigger_get_credentials(work_item
->trigger
),
186 LTTNG_OPTIONAL_GET_PTR(work_item
->object_creds
),
187 client_handle_transmission_status
,
191 static int action_executor_start_session_handler(struct action_executor
*executor
,
192 const struct action_work_item
*work_item
,
193 const struct lttng_action
*action
)
196 const char *session_name
;
197 enum lttng_action_status action_status
;
198 struct ltt_session
*session
;
199 enum lttng_error_code cmd_ret
;
201 action_status
= lttng_action_start_session_get_session_name(
202 action
, &session_name
);
203 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
204 ERR("Failed to get session name from `%s` action",
205 get_action_name(action
));
211 session
= session_find_by_name(session_name
);
213 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
214 session_name
, get_action_name(action
),
216 goto error_unlock_list
;
219 session_lock(session
);
220 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
221 goto error_dispose_session
;
224 cmd_ret
= cmd_start_trace(session
);
227 DBG("Successfully started session `%s` on behalf of trigger `%p`",
228 session_name
, work_item
->trigger
);
230 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
231 DBG("Attempted to start session `%s` on behalf of trigger `%p` but it was already started",
232 session_name
, work_item
->trigger
);
235 WARN("Failed to start session `%s` on behalf of trigger `%p`: %s",
236 session_name
, work_item
->trigger
,
237 lttng_strerror(-cmd_ret
));
241 error_dispose_session
:
242 session_unlock(session
);
243 session_put(session
);
245 session_unlock_list();
250 static int action_executor_stop_session_handler(struct action_executor
*executor
,
251 const struct action_work_item
*work_item
,
252 const struct lttng_action
*action
)
255 const char *session_name
;
256 enum lttng_action_status action_status
;
257 struct ltt_session
*session
;
258 enum lttng_error_code cmd_ret
;
260 action_status
= lttng_action_stop_session_get_session_name(
261 action
, &session_name
);
262 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
263 ERR("Failed to get session name from `%s` action",
264 get_action_name(action
));
270 session
= session_find_by_name(session_name
);
272 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
273 session_name
, get_action_name(action
),
275 goto error_unlock_list
;
278 session_lock(session
);
279 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
280 goto error_dispose_session
;
283 cmd_ret
= cmd_stop_trace(session
);
286 DBG("Successfully stopped session `%s` on behalf of trigger `%p`",
287 session_name
, work_item
->trigger
);
289 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
290 DBG("Attempted to stop session `%s` on behalf of trigger `%p` but it was already stopped",
291 session_name
, work_item
->trigger
);
294 WARN("Failed to stop session `%s` on behalf of trigger `%p`: %s",
295 session_name
, work_item
->trigger
,
296 lttng_strerror(-cmd_ret
));
300 error_dispose_session
:
301 session_unlock(session
);
302 session_put(session
);
304 session_unlock_list();
309 static int action_executor_rotate_session_handler(struct action_executor
*executor
,
310 const struct action_work_item
*work_item
,
311 const struct lttng_action
*action
)
314 const char *session_name
;
315 enum lttng_action_status action_status
;
316 struct ltt_session
*session
;
317 enum lttng_error_code cmd_ret
;
319 action_status
= lttng_action_rotate_session_get_session_name(
320 action
, &session_name
);
321 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
322 ERR("Failed to get session name from `%s` action",
323 get_action_name(action
));
329 session
= session_find_by_name(session_name
);
331 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
332 session_name
, get_action_name(action
),
334 goto error_unlock_list
;
337 session_lock(session
);
338 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
339 goto error_dispose_session
;
342 cmd_ret
= cmd_rotate_session(session
, NULL
, false,
343 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
346 DBG("Successfully started rotation of session `%s` on behalf of trigger `%p`",
347 session_name
, work_item
->trigger
);
349 case LTTNG_ERR_ROTATION_PENDING
:
350 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%p` but a rotation is already ongoing",
351 session_name
, work_item
->trigger
);
353 case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
:
354 case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
:
355 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%p` but a rotation has already been completed since the last stop or clear",
356 session_name
, work_item
->trigger
);
359 WARN("Failed to start a rotation of session `%s` on behalf of trigger `%p`: %s",
360 session_name
, work_item
->trigger
,
361 lttng_strerror(-cmd_ret
));
365 error_dispose_session
:
366 session_unlock(session
);
367 session_put(session
);
369 session_unlock_list();
374 static int action_executor_snapshot_session_handler(struct action_executor
*executor
,
375 const struct action_work_item
*work_item
,
376 const struct lttng_action
*action
)
379 const char *session_name
;
380 enum lttng_action_status action_status
;
381 struct ltt_session
*session
;
382 const struct lttng_snapshot_output default_snapshot_output
= {
383 .max_size
= UINT64_MAX
,
385 const struct lttng_snapshot_output
*snapshot_output
=
386 &default_snapshot_output
;
387 enum lttng_error_code cmd_ret
;
389 action_status
= lttng_action_snapshot_session_get_session_name(
390 action
, &session_name
);
391 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
392 ERR("Failed to get session name from `%s` action",
393 get_action_name(action
));
398 action_status
= lttng_action_snapshot_session_get_output(
399 action
, &snapshot_output
);
400 if (action_status
!= LTTNG_ACTION_STATUS_OK
&&
401 action_status
!= LTTNG_ACTION_STATUS_UNSET
) {
402 ERR("Failed to get output from `%s` action",
403 get_action_name(action
));
409 session
= session_find_by_name(session_name
);
411 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
412 session_name
, get_action_name(action
),
414 goto error_unlock_list
;
418 session_lock(session
);
419 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
420 goto error_dispose_session
;
423 cmd_ret
= cmd_snapshot_record(session
, snapshot_output
, 0);
426 DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%p`",
427 session_name
, work_item
->trigger
);
430 WARN("Failed to record snapshot of session `%s` on behalf of trigger `%p`: %s",
431 session_name
, work_item
->trigger
,
432 lttng_strerror(-cmd_ret
));
436 error_dispose_session
:
437 session_unlock(session
);
438 session_put(session
);
440 session_unlock_list();
445 static int action_executor_group_handler(struct action_executor
*executor
,
446 const struct action_work_item
*work_item
,
447 const struct lttng_action
*action_group
)
450 unsigned int i
, count
;
451 enum lttng_action_status action_status
;
453 action_status
= lttng_action_group_get_count(action_group
, &count
);
454 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
456 ERR("Failed to get count of action in action group");
461 DBG("Action group has %u action%s", count
, count
!= 1 ? "s" : "");
462 for (i
= 0; i
< count
; i
++) {
463 const struct lttng_action
*action
=
464 lttng_action_group_get_at_index(
467 ret
= action_executor_generic_handler(
468 executor
, work_item
, action
);
470 ERR("Stopping the execution of the action group of trigger `%p` following a fatal error",
479 static int action_executor_generic_handler(struct action_executor
*executor
,
480 const struct action_work_item
*work_item
,
481 const struct lttng_action
*action
)
483 DBG("Executing action `%s` of trigger `%p` action work item %" PRIu64
,
484 get_action_name(action
),
488 return action_executors
[lttng_action_get_type(action
)](
489 executor
, work_item
, action
);
492 static int action_work_item_execute(struct action_executor
*executor
,
493 struct action_work_item
*work_item
)
496 const struct lttng_action
*action
=
497 lttng_trigger_get_const_action(work_item
->trigger
);
499 DBG("Starting execution of action work item %" PRIu64
" of trigger `%p`",
500 work_item
->id
, work_item
->trigger
);
501 ret
= action_executor_generic_handler(executor
, work_item
, action
);
502 DBG("Completed execution of action work item %" PRIu64
" of trigger `%p`",
503 work_item
->id
, work_item
->trigger
);
507 static void action_work_item_destroy(struct action_work_item
*work_item
)
509 lttng_trigger_put(work_item
->trigger
);
510 lttng_evaluation_destroy(work_item
->evaluation
);
511 notification_client_list_put(work_item
->client_list
);
515 static void *action_executor_thread(void *_data
)
517 struct action_executor
*executor
= _data
;
521 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR
);
523 rcu_register_thread();
526 DBG("Entering work execution loop");
527 pthread_mutex_lock(&executor
->work
.lock
);
528 while (!executor
->should_quit
) {
530 struct action_work_item
*work_item
;
532 health_code_update();
533 if (executor
->work
.pending_count
== 0) {
535 DBG("No work items enqueued, entering wait");
536 pthread_cond_wait(&executor
->work
.cond
,
537 &executor
->work
.lock
);
538 DBG("Woke-up from wait");
543 /* Pop item from front of the listwith work lock held. */
544 work_item
= cds_list_first_entry(&executor
->work
.list
,
545 struct action_work_item
, list_node
);
546 cds_list_del(&work_item
->list_node
);
547 executor
->work
.pending_count
--;
550 * Work can be performed without holding the work lock,
551 * allowing new items to be queued.
553 pthread_mutex_unlock(&executor
->work
.lock
);
554 ret
= action_work_item_execute(executor
, work_item
);
555 action_work_item_destroy(work_item
);
561 health_code_update();
562 pthread_mutex_lock(&executor
->work
.lock
);
565 if (executor
->should_quit
) {
566 pthread_mutex_unlock(&executor
->work
.lock
);
568 DBG("Left work execution loop");
570 health_code_update();
572 rcu_thread_offline();
573 rcu_unregister_thread();
574 health_unregister(health_sessiond
);
579 static bool shutdown_action_executor_thread(void *_data
)
581 struct action_executor
*executor
= _data
;
583 executor
->should_quit
= true;
584 pthread_cond_signal(&executor
->work
.cond
);
588 static void clean_up_action_executor_thread(void *_data
)
590 struct action_executor
*executor
= _data
;
592 assert(cds_list_empty(&executor
->work
.list
));
594 pthread_mutex_destroy(&executor
->work
.lock
);
595 pthread_cond_destroy(&executor
->work
.cond
);
599 struct action_executor
*action_executor_create(
600 struct notification_thread_handle
*handle
)
602 struct action_executor
*executor
= zmalloc(sizeof(*executor
));
608 CDS_INIT_LIST_HEAD(&executor
->work
.list
);
609 pthread_cond_init(&executor
->work
.cond
, NULL
);
610 pthread_mutex_init(&executor
->work
.lock
, NULL
);
611 executor
->notification_thread_handle
= handle
;
613 executor
->thread
= lttng_thread_create(THREAD_NAME
,
614 action_executor_thread
, shutdown_action_executor_thread
,
615 clean_up_action_executor_thread
, executor
);
620 void action_executor_destroy(struct action_executor
*executor
)
622 struct action_work_item
*work_item
, *tmp
;
624 /* TODO Wait for work list to drain? */
625 lttng_thread_shutdown(executor
->thread
);
626 pthread_mutex_lock(&executor
->work
.lock
);
627 if (executor
->work
.pending_count
!= 0) {
629 " trigger action%s still queued for execution and will be discarded",
630 executor
->work
.pending_count
,
631 executor
->work
.pending_count
== 1 ? " is" :
635 cds_list_for_each_entry_safe (
636 work_item
, tmp
, &executor
->work
.list
, list_node
) {
637 WARN("Discarding action work item %" PRIu64
638 " associated to trigger `%p`",
639 work_item
->id
, work_item
->trigger
);
640 cds_list_del(&work_item
->list_node
);
641 action_work_item_destroy(work_item
);
643 pthread_mutex_unlock(&executor
->work
.lock
);
644 lttng_thread_put(executor
->thread
);
647 /* RCU read-lock must be held by the caller. */
648 enum action_executor_status
action_executor_enqueue(
649 struct action_executor
*executor
,
650 struct lttng_trigger
*trigger
,
651 struct lttng_evaluation
*evaluation
,
652 const struct lttng_credentials
*object_creds
,
653 struct notification_client_list
*client_list
)
655 enum action_executor_status executor_status
= ACTION_EXECUTOR_STATUS_OK
;
656 const uint64_t work_item_id
= executor
->next_work_item_id
++;
657 struct action_work_item
*work_item
;
660 pthread_mutex_lock(&executor
->work
.lock
);
661 /* Check for queue overflow. */
662 if (executor
->work
.pending_count
>= MAX_QUEUED_WORK_COUNT
) {
663 /* Most likely spammy, remove if it is the case. */
664 DBG("Refusing to enqueue action for trigger `%p` as work item %" PRIu64
666 trigger
, work_item_id
);
667 executor_status
= ACTION_EXECUTOR_STATUS_OVERFLOW
;
671 work_item
= zmalloc(sizeof(*work_item
));
673 PERROR("Failed to allocate action executor work item on behalf of trigger `%p`",
675 executor_status
= ACTION_EXECUTOR_STATUS_ERROR
;
679 lttng_trigger_get(trigger
);
681 const bool reference_acquired
=
682 notification_client_list_get(client_list
);
684 assert(reference_acquired
);
687 *work_item
= (typeof(*work_item
)){
690 /* Ownership transferred to the work item. */
691 .evaluation
= evaluation
,
693 .is_set
= !!object_creds
,
694 .value
= object_creds
? *object_creds
:
695 (typeof(work_item
->object_creds
.value
)) {},
697 .client_list
= client_list
,
698 .list_node
= CDS_LIST_HEAD_INIT(work_item
->list_node
),
702 cds_list_add_tail(&work_item
->list_node
, &executor
->work
.list
);
703 executor
->work
.pending_count
++;
704 DBG("Enqueued action for trigger `%p` as work item %" PRIu64
,
705 trigger
, work_item_id
);
709 pthread_mutex_unlock(&executor
->work
.lock
);
711 pthread_cond_signal(&executor
->work
.cond
);
714 lttng_evaluation_destroy(evaluation
);
715 return executor_status
;