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 const enum lttng_action_type action_type
= lttng_action_get_type(action
);
109 assert(action_type
!= LTTNG_ACTION_TYPE_UNKNOWN
);
111 return action_type_names
[action_type
];
114 /* Check if this trigger allowed to interect with a given session. */
115 static bool is_trigger_allowed_for_session(const struct lttng_trigger
*trigger
,
116 struct ltt_session
*session
)
118 bool is_allowed
= false;
119 const struct lttng_credentials session_creds
= {
120 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
121 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
123 /* Can never be NULL. */
124 const struct lttng_credentials
*trigger_creds
=
125 lttng_trigger_get_credentials(trigger
);
127 is_allowed
= (lttng_credentials_is_equal_uid(trigger_creds
, &session_creds
)) ||
128 (lttng_credentials_get_uid(trigger_creds
) == 0);
130 WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld",
132 (long int) session
->uid
,
133 (long int) session
->gid
,
134 (long int) lttng_credentials_get_uid(trigger_creds
));
140 static int client_handle_transmission_status(
141 struct notification_client
*client
,
142 enum client_transmission_status status
,
146 struct action_executor
*executor
= user_data
;
147 bool update_communication
= true;
150 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
151 DBG("Successfully sent full notification to client, client_id = %" PRIu64
,
153 update_communication
= false;
155 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
156 DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64
,
159 case CLIENT_TRANSMISSION_STATUS_FAIL
:
160 DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64
,
164 ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64
,
170 if (!update_communication
) {
174 /* Safe to read client's id without locking as it is immutable. */
175 ret
= notification_thread_client_communication_update(
176 executor
->notification_thread_handle
, client
->id
,
182 static int action_executor_notify_handler(struct action_executor
*executor
,
183 const struct action_work_item
*work_item
,
184 const struct lttng_action
*action
)
186 return notification_client_list_send_evaluation(work_item
->client_list
,
187 lttng_trigger_get_const_condition(work_item
->trigger
),
188 work_item
->evaluation
,
189 lttng_trigger_get_credentials(work_item
->trigger
),
190 LTTNG_OPTIONAL_GET_PTR(work_item
->object_creds
),
191 client_handle_transmission_status
, executor
);
194 static int action_executor_start_session_handler(struct action_executor
*executor
,
195 const struct action_work_item
*work_item
,
196 const struct lttng_action
*action
)
199 const char *session_name
;
200 enum lttng_action_status action_status
;
201 struct ltt_session
*session
;
202 enum lttng_error_code cmd_ret
;
204 action_status
= lttng_action_start_session_get_session_name(
205 action
, &session_name
);
206 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
207 ERR("Failed to get session name from `%s` action",
208 get_action_name(action
));
214 session
= session_find_by_name(session_name
);
216 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
217 session_name
, get_action_name(action
),
219 goto error_unlock_list
;
222 session_lock(session
);
223 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
224 goto error_dispose_session
;
227 cmd_ret
= cmd_start_trace(session
);
230 DBG("Successfully started session `%s` on behalf of trigger `%p`",
231 session_name
, work_item
->trigger
);
233 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
234 DBG("Attempted to start session `%s` on behalf of trigger `%p` but it was already started",
235 session_name
, work_item
->trigger
);
238 WARN("Failed to start session `%s` on behalf of trigger `%p`: %s",
239 session_name
, work_item
->trigger
,
240 lttng_strerror(-cmd_ret
));
244 error_dispose_session
:
245 session_unlock(session
);
246 session_put(session
);
248 session_unlock_list();
253 static int action_executor_stop_session_handler(struct action_executor
*executor
,
254 const struct action_work_item
*work_item
,
255 const struct lttng_action
*action
)
258 const char *session_name
;
259 enum lttng_action_status action_status
;
260 struct ltt_session
*session
;
261 enum lttng_error_code cmd_ret
;
263 action_status
= lttng_action_stop_session_get_session_name(
264 action
, &session_name
);
265 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
266 ERR("Failed to get session name from `%s` action",
267 get_action_name(action
));
273 session
= session_find_by_name(session_name
);
275 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
276 session_name
, get_action_name(action
),
278 goto error_unlock_list
;
281 session_lock(session
);
282 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
283 goto error_dispose_session
;
286 cmd_ret
= cmd_stop_trace(session
);
289 DBG("Successfully stopped session `%s` on behalf of trigger `%p`",
290 session_name
, work_item
->trigger
);
292 case LTTNG_ERR_TRACE_ALREADY_STOPPED
:
293 DBG("Attempted to stop session `%s` on behalf of trigger `%p` but it was already stopped",
294 session_name
, work_item
->trigger
);
297 WARN("Failed to stop session `%s` on behalf of trigger `%p`: %s",
298 session_name
, work_item
->trigger
,
299 lttng_strerror(-cmd_ret
));
303 error_dispose_session
:
304 session_unlock(session
);
305 session_put(session
);
307 session_unlock_list();
312 static int action_executor_rotate_session_handler(struct action_executor
*executor
,
313 const struct action_work_item
*work_item
,
314 const struct lttng_action
*action
)
317 const char *session_name
;
318 enum lttng_action_status action_status
;
319 struct ltt_session
*session
;
320 enum lttng_error_code cmd_ret
;
322 action_status
= lttng_action_rotate_session_get_session_name(
323 action
, &session_name
);
324 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
325 ERR("Failed to get session name from `%s` action",
326 get_action_name(action
));
332 session
= session_find_by_name(session_name
);
334 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
335 session_name
, get_action_name(action
),
337 goto error_unlock_list
;
340 session_lock(session
);
341 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
342 goto error_dispose_session
;
345 cmd_ret
= cmd_rotate_session(session
, NULL
, false,
346 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
349 DBG("Successfully started rotation of session `%s` on behalf of trigger `%p`",
350 session_name
, work_item
->trigger
);
352 case LTTNG_ERR_ROTATION_PENDING
:
353 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%p` but a rotation is already ongoing",
354 session_name
, work_item
->trigger
);
356 case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
:
357 case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
:
358 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",
359 session_name
, work_item
->trigger
);
362 WARN("Failed to start a rotation of session `%s` on behalf of trigger `%p`: %s",
363 session_name
, work_item
->trigger
,
364 lttng_strerror(-cmd_ret
));
368 error_dispose_session
:
369 session_unlock(session
);
370 session_put(session
);
372 session_unlock_list();
377 static int action_executor_snapshot_session_handler(struct action_executor
*executor
,
378 const struct action_work_item
*work_item
,
379 const struct lttng_action
*action
)
382 const char *session_name
;
383 enum lttng_action_status action_status
;
384 struct ltt_session
*session
;
385 const struct lttng_snapshot_output default_snapshot_output
= {
386 .max_size
= UINT64_MAX
,
388 const struct lttng_snapshot_output
*snapshot_output
=
389 &default_snapshot_output
;
390 enum lttng_error_code cmd_ret
;
392 action_status
= lttng_action_snapshot_session_get_session_name(
393 action
, &session_name
);
394 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
395 ERR("Failed to get session name from `%s` action",
396 get_action_name(action
));
401 action_status
= lttng_action_snapshot_session_get_output(
402 action
, &snapshot_output
);
403 if (action_status
!= LTTNG_ACTION_STATUS_OK
&&
404 action_status
!= LTTNG_ACTION_STATUS_UNSET
) {
405 ERR("Failed to get output from `%s` action",
406 get_action_name(action
));
412 session
= session_find_by_name(session_name
);
414 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
415 session_name
, get_action_name(action
),
417 goto error_unlock_list
;
421 session_lock(session
);
422 if (!is_trigger_allowed_for_session(work_item
->trigger
, session
)) {
423 goto error_dispose_session
;
426 cmd_ret
= cmd_snapshot_record(session
, snapshot_output
, 0);
429 DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%p`",
430 session_name
, work_item
->trigger
);
433 WARN("Failed to record snapshot of session `%s` on behalf of trigger `%p`: %s",
434 session_name
, work_item
->trigger
,
435 lttng_strerror(-cmd_ret
));
439 error_dispose_session
:
440 session_unlock(session
);
441 session_put(session
);
443 session_unlock_list();
448 static int action_executor_group_handler(struct action_executor
*executor
,
449 const struct action_work_item
*work_item
,
450 const struct lttng_action
*action_group
)
453 unsigned int i
, count
;
454 enum lttng_action_status action_status
;
456 action_status
= lttng_action_group_get_count(action_group
, &count
);
457 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
459 ERR("Failed to get count of action in action group");
464 DBG("Action group has %u action%s", count
, count
!= 1 ? "s" : "");
465 for (i
= 0; i
< count
; i
++) {
466 const struct lttng_action
*action
=
467 lttng_action_group_get_at_index(
470 ret
= action_executor_generic_handler(
471 executor
, work_item
, action
);
473 ERR("Stopping the execution of the action group of trigger `%p` following a fatal error",
482 static int action_executor_generic_handler(struct action_executor
*executor
,
483 const struct action_work_item
*work_item
,
484 const struct lttng_action
*action
)
486 const enum lttng_action_type action_type
= lttng_action_get_type(action
);
488 assert(action_type
!= LTTNG_ACTION_TYPE_UNKNOWN
);
490 DBG("Executing action `%s` of trigger `%p` action work item %" PRIu64
,
491 get_action_name(action
),
495 return action_executors
[action_type
](
496 executor
, work_item
, action
);
499 static int action_work_item_execute(struct action_executor
*executor
,
500 struct action_work_item
*work_item
)
503 const struct lttng_action
*action
=
504 lttng_trigger_get_const_action(work_item
->trigger
);
506 DBG("Starting execution of action work item %" PRIu64
" of trigger `%p`",
507 work_item
->id
, work_item
->trigger
);
508 ret
= action_executor_generic_handler(executor
, work_item
, action
);
509 DBG("Completed execution of action work item %" PRIu64
" of trigger `%p`",
510 work_item
->id
, work_item
->trigger
);
514 static void action_work_item_destroy(struct action_work_item
*work_item
)
516 lttng_trigger_put(work_item
->trigger
);
517 lttng_evaluation_destroy(work_item
->evaluation
);
518 notification_client_list_put(work_item
->client_list
);
522 static void *action_executor_thread(void *_data
)
524 struct action_executor
*executor
= _data
;
528 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR
);
530 rcu_register_thread();
533 DBG("Entering work execution loop");
534 pthread_mutex_lock(&executor
->work
.lock
);
535 while (!executor
->should_quit
) {
537 struct action_work_item
*work_item
;
539 health_code_update();
540 if (executor
->work
.pending_count
== 0) {
542 DBG("No work items enqueued, entering wait");
543 pthread_cond_wait(&executor
->work
.cond
,
544 &executor
->work
.lock
);
545 DBG("Woke-up from wait");
550 /* Pop item from front of the list with work lock held. */
551 work_item
= cds_list_first_entry(&executor
->work
.list
,
552 struct action_work_item
, list_node
);
553 cds_list_del(&work_item
->list_node
);
554 executor
->work
.pending_count
--;
557 * Work can be performed without holding the work lock,
558 * allowing new items to be queued.
560 pthread_mutex_unlock(&executor
->work
.lock
);
561 ret
= action_work_item_execute(executor
, work_item
);
562 action_work_item_destroy(work_item
);
568 health_code_update();
569 pthread_mutex_lock(&executor
->work
.lock
);
572 if (executor
->should_quit
) {
573 pthread_mutex_unlock(&executor
->work
.lock
);
575 DBG("Left work execution loop");
577 health_code_update();
579 rcu_thread_offline();
580 rcu_unregister_thread();
581 health_unregister(health_sessiond
);
586 static bool shutdown_action_executor_thread(void *_data
)
588 struct action_executor
*executor
= _data
;
590 executor
->should_quit
= true;
591 pthread_cond_signal(&executor
->work
.cond
);
595 static void clean_up_action_executor_thread(void *_data
)
597 struct action_executor
*executor
= _data
;
599 assert(cds_list_empty(&executor
->work
.list
));
601 pthread_mutex_destroy(&executor
->work
.lock
);
602 pthread_cond_destroy(&executor
->work
.cond
);
606 struct action_executor
*action_executor_create(
607 struct notification_thread_handle
*handle
)
609 struct action_executor
*executor
= zmalloc(sizeof(*executor
));
615 CDS_INIT_LIST_HEAD(&executor
->work
.list
);
616 pthread_cond_init(&executor
->work
.cond
, NULL
);
617 pthread_mutex_init(&executor
->work
.lock
, NULL
);
618 executor
->notification_thread_handle
= handle
;
620 executor
->thread
= lttng_thread_create(THREAD_NAME
,
621 action_executor_thread
, shutdown_action_executor_thread
,
622 clean_up_action_executor_thread
, executor
);
627 void action_executor_destroy(struct action_executor
*executor
)
629 struct action_work_item
*work_item
, *tmp
;
631 /* TODO Wait for work list to drain? */
632 lttng_thread_shutdown(executor
->thread
);
633 pthread_mutex_lock(&executor
->work
.lock
);
634 if (executor
->work
.pending_count
!= 0) {
636 " trigger action%s still queued for execution and will be discarded",
637 executor
->work
.pending_count
,
638 executor
->work
.pending_count
== 1 ? " is" :
642 cds_list_for_each_entry_safe (
643 work_item
, tmp
, &executor
->work
.list
, list_node
) {
644 WARN("Discarding action work item %" PRIu64
645 " associated to trigger `%p`",
646 work_item
->id
, work_item
->trigger
);
647 cds_list_del(&work_item
->list_node
);
648 action_work_item_destroy(work_item
);
650 pthread_mutex_unlock(&executor
->work
.lock
);
651 lttng_thread_put(executor
->thread
);
654 /* RCU read-lock must be held by the caller. */
655 enum action_executor_status
action_executor_enqueue(
656 struct action_executor
*executor
,
657 struct lttng_trigger
*trigger
,
658 struct lttng_evaluation
*evaluation
,
659 const struct lttng_credentials
*object_creds
,
660 struct notification_client_list
*client_list
)
662 enum action_executor_status executor_status
= ACTION_EXECUTOR_STATUS_OK
;
663 const uint64_t work_item_id
= executor
->next_work_item_id
++;
664 struct action_work_item
*work_item
;
667 pthread_mutex_lock(&executor
->work
.lock
);
668 /* Check for queue overflow. */
669 if (executor
->work
.pending_count
>= MAX_QUEUED_WORK_COUNT
) {
670 /* Most likely spammy, remove if it is the case. */
671 DBG("Refusing to enqueue action for trigger `%p` as work item %" PRIu64
672 " (overflow)", trigger
, work_item_id
);
673 executor_status
= ACTION_EXECUTOR_STATUS_OVERFLOW
;
677 work_item
= zmalloc(sizeof(*work_item
));
679 PERROR("Failed to allocate action executor work item on behalf of trigger `%p`",
681 executor_status
= ACTION_EXECUTOR_STATUS_ERROR
;
685 lttng_trigger_get(trigger
);
687 const bool reference_acquired
=
688 notification_client_list_get(client_list
);
690 assert(reference_acquired
);
693 *work_item
= (typeof(*work_item
)){
696 /* Ownership transferred to the work item. */
697 .evaluation
= evaluation
,
699 .is_set
= !!object_creds
,
700 .value
= object_creds
? *object_creds
:
701 (typeof(work_item
->object_creds
.value
)) {},
703 .client_list
= client_list
,
704 .list_node
= CDS_LIST_HEAD_INIT(work_item
->list_node
),
708 cds_list_add_tail(&work_item
->list_node
, &executor
->work
.list
);
709 executor
->work
.pending_count
++;
710 DBG("Enqueued action for trigger `%p` as work item %" PRIu64
,
711 trigger
, work_item_id
);
715 pthread_mutex_unlock(&executor
->work
.lock
);
717 pthread_cond_signal(&executor
->work
.cond
);
720 lttng_evaluation_destroy(evaluation
);
721 return executor_status
;