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
= {
119 /* Can never be NULL. */
120 const struct lttng_credentials
*trigger_creds
=
121 lttng_trigger_get_credentials(trigger
);
123 is_allowed
= (trigger_creds
->uid
== session_creds
.uid
) ||
124 (trigger_creds
->uid
== 0);
126 WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld, trigger gid = %ld",
128 (long int) session
->uid
,
129 (long int) session
->gid
,
130 (long int) trigger_creds
->uid
,
131 (long int) trigger_creds
->gid
);
137 static int client_handle_transmission_status(
138 struct notification_client
*client
,
139 enum client_transmission_status status
,
143 struct action_executor
*executor
= user_data
;
144 bool update_communication
= true;
147 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
148 DBG("Successfully sent full notification to client, client_id = %" PRIu64
,
150 update_communication
= false;
152 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
153 DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64
,
156 case CLIENT_TRANSMISSION_STATUS_FAIL
:
157 DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64
,
161 ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64
,
167 if (!update_communication
) {
171 /* Safe to read client's id without locking as it is immutable. */
172 ret
= notification_thread_client_communication_update(
173 executor
->notification_thread_handle
, client
->id
,
179 static int action_executor_notify_handler(struct action_executor
*executor
,
180 const struct action_work_item
*work_item
,
181 const struct lttng_action
*action
)
183 return notification_client_list_send_evaluation(work_item
->client_list
,
184 lttng_trigger_get_const_condition(work_item
->trigger
),
185 work_item
->evaluation
,
186 lttng_trigger_get_credentials(work_item
->trigger
),
187 LTTNG_OPTIONAL_GET_PTR(work_item
->object_creds
),
188 client_handle_transmission_status
,
192 static int action_executor_start_session_handler(struct action_executor
*executor
,
193 const struct action_work_item
*work_item
,
194 const struct lttng_action
*action
)
197 const char *session_name
;
198 enum lttng_action_status action_status
;
199 struct ltt_session
*session
;
200 enum lttng_error_code cmd_ret
;
202 action_status
= lttng_action_start_session_get_session_name(
203 action
, &session_name
);
204 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
205 ERR("Failed to get session name from `%s` action",
206 get_action_name(action
));
212 session
= session_find_by_name(session_name
);
214 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
215 session_name
, get_action_name(action
),
217 goto error_unlock_list
;
220 session_lock(session
);
221 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
222 goto error_dispose_session
;
225 cmd_ret
= cmd_start_trace(session
);
228 DBG("Successfully started session `%s` on behalf of trigger `%p`",
229 session_name
, work_item
->trigger
);
231 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
232 DBG("Attempted to start session `%s` on behalf of trigger `%p` but it was already started",
233 session_name
, work_item
->trigger
);
236 WARN("Failed to start session `%s` on behalf of trigger `%p`: %s",
237 session_name
, work_item
->trigger
,
238 lttng_strerror(-cmd_ret
));
242 error_dispose_session
:
243 session_unlock(session
);
244 session_put(session
);
246 session_unlock_list();
251 static int action_executor_stop_session_handler(struct action_executor
*executor
,
252 const struct action_work_item
*work_item
,
253 const struct lttng_action
*action
)
256 const char *session_name
;
257 enum lttng_action_status action_status
;
258 struct ltt_session
*session
;
259 enum lttng_error_code cmd_ret
;
261 action_status
= lttng_action_stop_session_get_session_name(
262 action
, &session_name
);
263 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
264 ERR("Failed to get session name from `%s` action",
265 get_action_name(action
));
271 session
= session_find_by_name(session_name
);
273 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
274 session_name
, get_action_name(action
),
276 goto error_unlock_list
;
279 session_lock(session
);
280 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
281 goto error_dispose_session
;
284 cmd_ret
= cmd_stop_trace(session
);
287 DBG("Successfully stopped session `%s` on behalf of trigger `%p`",
288 session_name
, work_item
->trigger
);
290 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
291 DBG("Attempted to stop session `%s` on behalf of trigger `%p` but it was already stopped",
292 session_name
, work_item
->trigger
);
295 WARN("Failed to stop session `%s` on behalf of trigger `%p`: %s",
296 session_name
, work_item
->trigger
,
297 lttng_strerror(-cmd_ret
));
301 error_dispose_session
:
302 session_unlock(session
);
303 session_put(session
);
305 session_unlock_list();
310 static int action_executor_rotate_session_handler(struct action_executor
*executor
,
311 const struct action_work_item
*work_item
,
312 const struct lttng_action
*action
)
315 const char *session_name
;
316 enum lttng_action_status action_status
;
317 struct ltt_session
*session
;
318 enum lttng_error_code cmd_ret
;
320 action_status
= lttng_action_rotate_session_get_session_name(
321 action
, &session_name
);
322 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
323 ERR("Failed to get session name from `%s` action",
324 get_action_name(action
));
330 session
= session_find_by_name(session_name
);
332 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
333 session_name
, get_action_name(action
),
335 goto error_unlock_list
;
338 session_lock(session
);
339 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
340 goto error_dispose_session
;
343 cmd_ret
= cmd_rotate_session(session
, NULL
, false,
344 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
347 DBG("Successfully started rotation of session `%s` on behalf of trigger `%p`",
348 session_name
, work_item
->trigger
);
350 case LTTNG_ERR_ROTATION_PENDING
:
351 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%p` but a rotation is already ongoing",
352 session_name
, work_item
->trigger
);
354 case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
:
355 case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
:
356 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",
357 session_name
, work_item
->trigger
);
360 WARN("Failed to start a rotation of session `%s` on behalf of trigger `%p`: %s",
361 session_name
, work_item
->trigger
,
362 lttng_strerror(-cmd_ret
));
366 error_dispose_session
:
367 session_unlock(session
);
368 session_put(session
);
370 session_unlock_list();
375 static int action_executor_snapshot_session_handler(struct action_executor
*executor
,
376 const struct action_work_item
*work_item
,
377 const struct lttng_action
*action
)
380 const char *session_name
;
381 enum lttng_action_status action_status
;
382 struct ltt_session
*session
;
383 const struct lttng_snapshot_output default_snapshot_output
= {
384 .max_size
= UINT64_MAX
,
386 const struct lttng_snapshot_output
*snapshot_output
=
387 &default_snapshot_output
;
388 enum lttng_error_code cmd_ret
;
390 action_status
= lttng_action_snapshot_session_get_session_name(
391 action
, &session_name
);
392 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
393 ERR("Failed to get session name from `%s` action",
394 get_action_name(action
));
399 action_status
= lttng_action_snapshot_session_get_output(
400 action
, &snapshot_output
);
401 if (action_status
!= LTTNG_ACTION_STATUS_OK
&&
402 action_status
!= LTTNG_ACTION_STATUS_UNSET
) {
403 ERR("Failed to get output from `%s` action",
404 get_action_name(action
));
410 session
= session_find_by_name(session_name
);
412 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
413 session_name
, get_action_name(action
),
415 goto error_unlock_list
;
419 session_lock(session
);
420 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
421 goto error_dispose_session
;
424 cmd_ret
= cmd_snapshot_record(session
, snapshot_output
, 0);
427 DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%p`",
428 session_name
, work_item
->trigger
);
431 WARN("Failed to record snapshot of session `%s` on behalf of trigger `%p`: %s",
432 session_name
, work_item
->trigger
,
433 lttng_strerror(-cmd_ret
));
437 error_dispose_session
:
438 session_unlock(session
);
439 session_put(session
);
441 session_unlock_list();
446 static int action_executor_group_handler(struct action_executor
*executor
,
447 const struct action_work_item
*work_item
,
448 const struct lttng_action
*action_group
)
451 unsigned int i
, count
;
452 enum lttng_action_status action_status
;
454 action_status
= lttng_action_group_get_count(action_group
, &count
);
455 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
457 ERR("Failed to get count of action in action group");
462 DBG("Action group has %u action%s", count
, count
!= 1 ? "s" : "");
463 for (i
= 0; i
< count
; i
++) {
464 const struct lttng_action
*action
=
465 lttng_action_group_get_at_index(
468 ret
= action_executor_generic_handler(
469 executor
, work_item
, action
);
471 ERR("Stopping the execution of the action group of trigger `%p` following a fatal error",
480 static int action_executor_generic_handler(struct action_executor
*executor
,
481 const struct action_work_item
*work_item
,
482 const struct lttng_action
*action
)
484 DBG("Executing action `%s` of trigger `%p` action work item %" PRIu64
,
485 get_action_name(action
),
489 return action_executors
[lttng_action_get_type(action
)](
490 executor
, work_item
, action
);
493 static int action_work_item_execute(struct action_executor
*executor
,
494 struct action_work_item
*work_item
)
497 const struct lttng_action
*action
=
498 lttng_trigger_get_const_action(work_item
->trigger
);
500 DBG("Starting execution of action work item %" PRIu64
" of trigger `%p`",
501 work_item
->id
, work_item
->trigger
);
502 ret
= action_executor_generic_handler(executor
, work_item
, action
);
503 DBG("Completed execution of action work item %" PRIu64
" of trigger `%p`",
504 work_item
->id
, work_item
->trigger
);
508 static void action_work_item_destroy(struct action_work_item
*work_item
)
510 lttng_trigger_put(work_item
->trigger
);
511 lttng_evaluation_destroy(work_item
->evaluation
);
512 notification_client_list_put(work_item
->client_list
);
516 static void *action_executor_thread(void *_data
)
518 struct action_executor
*executor
= _data
;
522 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR
);
524 rcu_register_thread();
527 DBG("Entering work execution loop");
528 pthread_mutex_lock(&executor
->work
.lock
);
529 while (!executor
->should_quit
) {
531 struct action_work_item
*work_item
;
533 health_code_update();
534 if (executor
->work
.pending_count
== 0) {
536 DBG("No work items enqueued, entering wait");
537 pthread_cond_wait(&executor
->work
.cond
,
538 &executor
->work
.lock
);
539 DBG("Woke-up from wait");
544 /* Pop item from front of the listwith work lock held. */
545 work_item
= cds_list_first_entry(&executor
->work
.list
,
546 struct action_work_item
, list_node
);
547 cds_list_del(&work_item
->list_node
);
548 executor
->work
.pending_count
--;
551 * Work can be performed without holding the work lock,
552 * allowing new items to be queued.
554 pthread_mutex_unlock(&executor
->work
.lock
);
555 ret
= action_work_item_execute(executor
, work_item
);
556 action_work_item_destroy(work_item
);
562 health_code_update();
563 pthread_mutex_lock(&executor
->work
.lock
);
566 if (executor
->should_quit
) {
567 pthread_mutex_unlock(&executor
->work
.lock
);
569 DBG("Left work execution loop");
571 health_code_update();
573 rcu_thread_offline();
574 rcu_unregister_thread();
575 health_unregister(health_sessiond
);
580 static bool shutdown_action_executor_thread(void *_data
)
582 struct action_executor
*executor
= _data
;
584 executor
->should_quit
= true;
585 pthread_cond_signal(&executor
->work
.cond
);
589 static void clean_up_action_executor_thread(void *_data
)
591 struct action_executor
*executor
= _data
;
593 assert(cds_list_empty(&executor
->work
.list
));
595 pthread_mutex_destroy(&executor
->work
.lock
);
596 pthread_cond_destroy(&executor
->work
.cond
);
600 struct action_executor
*action_executor_create(
601 struct notification_thread_handle
*handle
)
603 struct action_executor
*executor
= zmalloc(sizeof(*executor
));
609 CDS_INIT_LIST_HEAD(&executor
->work
.list
);
610 pthread_cond_init(&executor
->work
.cond
, NULL
);
611 pthread_mutex_init(&executor
->work
.lock
, NULL
);
612 executor
->notification_thread_handle
= handle
;
614 executor
->thread
= lttng_thread_create(THREAD_NAME
,
615 action_executor_thread
, shutdown_action_executor_thread
,
616 clean_up_action_executor_thread
, executor
);
621 void action_executor_destroy(struct action_executor
*executor
)
623 struct action_work_item
*work_item
, *tmp
;
625 /* TODO Wait for work list to drain? */
626 lttng_thread_shutdown(executor
->thread
);
627 pthread_mutex_lock(&executor
->work
.lock
);
628 if (executor
->work
.pending_count
!= 0) {
630 " trigger action%s still queued for execution and will be discarded",
631 executor
->work
.pending_count
,
632 executor
->work
.pending_count
== 1 ? " is" :
636 cds_list_for_each_entry_safe (
637 work_item
, tmp
, &executor
->work
.list
, list_node
) {
638 WARN("Discarding action work item %" PRIu64
639 " associated to trigger `%p`",
640 work_item
->id
, work_item
->trigger
);
641 cds_list_del(&work_item
->list_node
);
642 action_work_item_destroy(work_item
);
644 pthread_mutex_unlock(&executor
->work
.lock
);
645 lttng_thread_put(executor
->thread
);
648 /* RCU read-lock must be held by the caller. */
649 enum action_executor_status
action_executor_enqueue(
650 struct action_executor
*executor
,
651 struct lttng_trigger
*trigger
,
652 struct lttng_evaluation
*evaluation
,
653 const struct lttng_credentials
*object_creds
,
654 struct notification_client_list
*client_list
)
656 enum action_executor_status executor_status
= ACTION_EXECUTOR_STATUS_OK
;
657 const uint64_t work_item_id
= executor
->next_work_item_id
++;
658 struct action_work_item
*work_item
;
661 pthread_mutex_lock(&executor
->work
.lock
);
662 /* Check for queue overflow. */
663 if (executor
->work
.pending_count
>= MAX_QUEUED_WORK_COUNT
) {
664 /* Most likely spammy, remove if it is the case. */
665 DBG("Refusing to enqueue action for trigger `%p` as work item %" PRIu64
667 trigger
, work_item_id
);
668 executor_status
= ACTION_EXECUTOR_STATUS_OVERFLOW
;
672 work_item
= zmalloc(sizeof(*work_item
));
674 PERROR("Failed to allocate action executor work item on behalf of trigger `%p`",
676 executor_status
= ACTION_EXECUTOR_STATUS_ERROR
;
680 lttng_trigger_get(trigger
);
682 const bool reference_acquired
=
683 notification_client_list_get(client_list
);
685 assert(reference_acquired
);
688 *work_item
= (typeof(*work_item
)){
691 /* Ownership transferred to the work item. */
692 .evaluation
= evaluation
,
694 .is_set
= !!object_creds
,
695 .value
= object_creds
? *object_creds
:
696 (typeof(work_item
->object_creds
.value
)) {},
698 .client_list
= client_list
,
699 .list_node
= CDS_LIST_HEAD_INIT(work_item
->list_node
),
703 cds_list_add_tail(&work_item
->list_node
, &executor
->work
.list
);
704 executor
->work
.pending_count
++;
705 DBG("Enqueued action for trigger `%p` as work item %" PRIu64
,
706 trigger
, work_item_id
);
710 pthread_mutex_unlock(&executor
->work
.lock
);
712 pthread_cond_signal(&executor
->work
.cond
);
715 lttng_evaluation_destroy(evaluation
);
716 return executor_status
;