Fix: sessiond: rotation thread: fatal error when not finding a session
[lttng-tools.git] / src / bin / lttng-sessiond / action-executor.cpp
CommitLineData
f2b3ef9f
JG
1/*
2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#include "action-executor.h"
9#include "cmd.h"
10#include "health-sessiond.h"
11#include "lttng-sessiond.h"
12#include "notification-thread-internal.h"
13#include "session.h"
14#include "thread.h"
72365501 15#include <common/dynamic-array.h>
f2b3ef9f
JG
16#include <common/macros.h>
17#include <common/optional.h>
18#include <lttng/action/action-internal.h>
ad63a966
JR
19#include <lttng/action/list-internal.h>
20#include <lttng/action/list.h>
2af3b9c9 21#include <lttng/action/notify-internal.h>
f2b3ef9f
JG
22#include <lttng/action/notify.h>
23#include <lttng/action/rotate-session.h>
24#include <lttng/action/snapshot-session.h>
25#include <lttng/action/start-session.h>
26#include <lttng/action/stop-session.h>
27#include <lttng/condition/evaluation.h>
670a26e4 28#include <lttng/condition/event-rule-matches-internal.h>
f2b3ef9f
JG
29#include <lttng/lttng-error.h>
30#include <lttng/trigger/trigger-internal.h>
31#include <pthread.h>
32#include <stdbool.h>
33#include <stddef.h>
34#include <urcu/list.h>
35
36#define THREAD_NAME "Action Executor"
37#define MAX_QUEUED_WORK_COUNT 8192
38
72365501
JR
39/*
40 * A work item is composed of a dynamic array of sub-items which
41 * represent a flattened, and augmented, version of a trigger's actions.
42 *
43 * We cannot rely solely on the trigger's actions since each action can have an
44 * execution context we need to comply with.
45 *
46 * The notion of execution context is required since for some actions the
47 * associated object are referenced by name and not by id. This can lead to
48 * a number of ambiguities when executing an action work item.
49 *
50 * For example, let's take a simple trigger such as:
51 * - condition: ust event a
52 * - action: start session S
53 *
54 * At time T, session S exists.
55 * At T + 1, the event A is hit.
56 * At T + 2, the tracer event notification is received and the work item is
57 * queued. Here session S have an id of 1.
58 * At T + 3, the session S is destroyed and a new session S is created, with a
59 * resulting id of 200.
60 * At T +4, the work item is popped from the queue and begin execution and will
61 * start session S with an id of 200 instead of the session S id 1 that was
62 * present at the queuing phase.
63 *
64 * The context to be respected is the one when the work item is queued. If the
65 * execution context is not the same at the moment of execution, we skip the
66 * execution of that sub-item.
67 *
68 * It is the same policy in regards to the validity of the associated
69 * trigger object at the moment of execution, if the trigger is found to be
70 * unregistered, the execution is skipped.
71 */
72
f2b3ef9f
JG
73struct action_work_item {
74 uint64_t id;
72365501
JR
75
76 /*
77 * The actions to be executed with their respective execution context.
78 * See struct `action_work_subitem`.
79 */
be65f802 80 struct lttng_dynamic_array subitems;
72365501
JR
81
82 /* Execution context data */
f2b3ef9f
JG
83 struct lttng_trigger *trigger;
84 struct lttng_evaluation *evaluation;
85 struct notification_client_list *client_list;
86 LTTNG_OPTIONAL(struct lttng_credentials) object_creds;
87 struct cds_list_head list_node;
88};
89
72365501
JR
90struct action_work_subitem {
91 struct lttng_action *action;
92 struct {
93 /* Used by actions targeting a session. */
94 LTTNG_OPTIONAL(uint64_t) session_id;
95 } context;
96};
97
f2b3ef9f
JG
98struct action_executor {
99 struct lttng_thread *thread;
100 struct notification_thread_handle *notification_thread_handle;
101 struct {
102 uint64_t pending_count;
103 struct cds_list_head list;
104 pthread_cond_t cond;
105 pthread_mutex_t lock;
106 } work;
107 bool should_quit;
108 uint64_t next_work_item_id;
109};
110
111/*
112 * Only return non-zero on a fatal error that should shut down the action
113 * executor.
114 */
115typedef int (*action_executor_handler)(struct action_executor *executor,
116 const struct action_work_item *,
72365501 117 struct action_work_subitem *item);
f2b3ef9f
JG
118
119static int action_executor_notify_handler(struct action_executor *executor,
120 const struct action_work_item *,
72365501 121 struct action_work_subitem *);
2d57482c
JR
122static int action_executor_start_session_handler(
123 struct action_executor *executor,
f2b3ef9f 124 const struct action_work_item *,
72365501 125 struct action_work_subitem *);
2d57482c
JR
126static int action_executor_stop_session_handler(
127 struct action_executor *executor,
f2b3ef9f 128 const struct action_work_item *,
72365501 129 struct action_work_subitem *);
2d57482c
JR
130static int action_executor_rotate_session_handler(
131 struct action_executor *executor,
f2b3ef9f 132 const struct action_work_item *,
72365501 133 struct action_work_subitem *);
2d57482c
JR
134static int action_executor_snapshot_session_handler(
135 struct action_executor *executor,
f2b3ef9f 136 const struct action_work_item *,
72365501 137 struct action_work_subitem *);
7c2fae7c 138static int action_executor_list_handler(struct action_executor *executor,
f2b3ef9f 139 const struct action_work_item *,
72365501 140 struct action_work_subitem *);
f2b3ef9f
JG
141static int action_executor_generic_handler(struct action_executor *executor,
142 const struct action_work_item *,
72365501 143 struct action_work_subitem *);
f2b3ef9f
JG
144
145static const action_executor_handler action_executors[] = {
7966af57
SM
146 action_executor_notify_handler,
147 action_executor_start_session_handler,
148 action_executor_stop_session_handler,
149 action_executor_rotate_session_handler,
150 action_executor_snapshot_session_handler,
151 action_executor_list_handler,
f2b3ef9f
JG
152};
153
72365501
JR
154/* Forward declaration */
155static int add_action_to_subitem_array(struct lttng_action *action,
156 struct lttng_dynamic_array *subitems);
157
158static int populate_subitem_array_from_trigger(struct lttng_trigger *trigger,
159 struct lttng_dynamic_array *subitems);
160
161static void action_work_subitem_destructor(void *element)
162{
7966af57 163 struct action_work_subitem *subitem = (action_work_subitem *) element;
72365501
JR
164
165 lttng_action_put(subitem->action);
166}
167
f2b3ef9f
JG
168static const char *get_action_name(const struct lttng_action *action)
169{
0e43bcbf
JG
170 const enum lttng_action_type action_type = lttng_action_get_type(action);
171
a0377dfe 172 LTTNG_ASSERT(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
0e43bcbf 173
c0e2990d 174 return lttng_action_type_string(action_type);
f2b3ef9f
JG
175}
176
177/* Check if this trigger allowed to interect with a given session. */
178static bool is_trigger_allowed_for_session(const struct lttng_trigger *trigger,
179 struct ltt_session *session)
180{
181 bool is_allowed = false;
182 const struct lttng_credentials session_creds = {
ff588497
JR
183 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
184 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
f2b3ef9f
JG
185 };
186 /* Can never be NULL. */
187 const struct lttng_credentials *trigger_creds =
188 lttng_trigger_get_credentials(trigger);
189
ff588497
JR
190 is_allowed = (lttng_credentials_is_equal_uid(trigger_creds, &session_creds)) ||
191 (lttng_credentials_get_uid(trigger_creds) == 0);
f2b3ef9f 192 if (!is_allowed) {
ff588497 193 WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld",
f2b3ef9f
JG
194 session->name,
195 (long int) session->uid,
196 (long int) session->gid,
ff588497 197 (long int) lttng_credentials_get_uid(trigger_creds));
f2b3ef9f
JG
198 }
199
200 return is_allowed;
201}
202
34f87583
JR
203static const char *get_trigger_name(const struct lttng_trigger *trigger)
204{
205 const char *trigger_name;
206 enum lttng_trigger_status trigger_status;
207
208 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
0efb2ad7
JG
209 switch (trigger_status) {
210 case LTTNG_TRIGGER_STATUS_OK:
211 break;
212 case LTTNG_TRIGGER_STATUS_UNSET:
213 trigger_name = "(anonymous)";
214 break;
215 default:
216 trigger_name = "(failed to get name)";
217 break;
218 }
34f87583
JR
219
220 return trigger_name;
221}
222
f2b3ef9f
JG
223static int client_handle_transmission_status(
224 struct notification_client *client,
225 enum client_transmission_status status,
226 void *user_data)
227{
228 int ret = 0;
7966af57 229 struct action_executor *executor = (action_executor *) user_data;
f2b3ef9f
JG
230 bool update_communication = true;
231
f2b3ef9f
JG
232 switch (status) {
233 case CLIENT_TRANSMISSION_STATUS_COMPLETE:
234 DBG("Successfully sent full notification to client, client_id = %" PRIu64,
235 client->id);
236 update_communication = false;
237 break;
238 case CLIENT_TRANSMISSION_STATUS_QUEUED:
239 DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64,
240 client->id);
241 break;
242 case CLIENT_TRANSMISSION_STATUS_FAIL:
243 DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64,
244 client->id);
f2b3ef9f
JG
245 break;
246 default:
247 ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64,
248 client->id);
f2b3ef9f
JG
249 ret = -1;
250 goto end;
251 }
252
253 if (!update_communication) {
254 goto end;
255 }
256
6c24d3fd 257 /* Safe to read client's id without locking as it is immutable. */
f2b3ef9f
JG
258 ret = notification_thread_client_communication_update(
259 executor->notification_thread_handle, client->id,
260 status);
261end:
262 return ret;
263}
264
265static int action_executor_notify_handler(struct action_executor *executor,
266 const struct action_work_item *work_item,
72365501 267 struct action_work_subitem *item)
f2b3ef9f
JG
268{
269 return notification_client_list_send_evaluation(work_item->client_list,
52d55cf9 270 work_item->trigger,
f2b3ef9f 271 work_item->evaluation,
c203f058
JR
272 work_item->object_creds.is_set ?
273 &(work_item->object_creds.value) :
274 NULL,
64eafdf6 275 client_handle_transmission_status, executor);
f2b3ef9f
JG
276}
277
2d57482c
JR
278static int action_executor_start_session_handler(
279 struct action_executor *executor,
f2b3ef9f 280 const struct action_work_item *work_item,
72365501 281 struct action_work_subitem *item)
f2b3ef9f
JG
282{
283 int ret = 0;
284 const char *session_name;
285 enum lttng_action_status action_status;
286 struct ltt_session *session;
287 enum lttng_error_code cmd_ret;
72365501 288 struct lttng_action *action = item->action;
f2b3ef9f
JG
289
290 action_status = lttng_action_start_session_get_session_name(
291 action, &session_name);
292 if (action_status != LTTNG_ACTION_STATUS_OK) {
293 ERR("Failed to get session name from `%s` action",
294 get_action_name(action));
295 ret = -1;
296 goto end;
297 }
298
72365501
JR
299 /*
300 * Validate if at the moment of the action was queued the session
301 * existed. If not skip the action altogether.
302 */
303 if (!item->context.session_id.is_set) {
4ec6f5b6 304 DBG("Session `%s` was not present at the moment the work item was enqueued for `%s` action of trigger `%s`",
72365501
JR
305 session_name, get_action_name(action),
306 get_trigger_name(work_item->trigger));
307 lttng_action_increase_execution_failure_count(action);
72365501
JR
308 goto end;
309 }
310
f2b3ef9f 311 session_lock_list();
4a467a84 312 session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
f2b3ef9f 313 if (!session) {
34f87583 314 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
f2b3ef9f 315 session_name, get_action_name(action),
34f87583 316 get_trigger_name(work_item->trigger));
4a467a84 317 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
318 goto error_unlock_list;
319 }
320
4a467a84
JR
321 session_lock(session);
322 if (session->destroyed) {
4ec6f5b6 323 DBG("Session `%s` with id = %" PRIu64 " is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
4a467a84 324 session->name, session->id,
72365501
JR
325 get_action_name(action),
326 get_trigger_name(work_item->trigger));
4a467a84 327 goto error_unlock_session;
72365501
JR
328 }
329
f2b3ef9f 330 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
635e0160 331 goto error_unlock_session;
f2b3ef9f
JG
332 }
333
7966af57 334 cmd_ret = (lttng_error_code) cmd_start_trace(session);
f2b3ef9f
JG
335 switch (cmd_ret) {
336 case LTTNG_OK:
34f87583
JR
337 DBG("Successfully started session `%s` on behalf of trigger `%s`",
338 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
339 break;
340 case LTTNG_ERR_TRACE_ALREADY_STARTED:
34f87583
JR
341 DBG("Attempted to start session `%s` on behalf of trigger `%s` but it was already started",
342 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
343 break;
344 default:
34f87583
JR
345 WARN("Failed to start session `%s` on behalf of trigger `%s`: %s",
346 session_name, get_trigger_name(work_item->trigger),
f2b3ef9f 347 lttng_strerror(-cmd_ret));
2d57482c 348 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
349 break;
350 }
351
635e0160 352error_unlock_session:
f2b3ef9f
JG
353 session_unlock(session);
354 session_put(session);
355error_unlock_list:
356 session_unlock_list();
357end:
358 return ret;
359}
360
2d57482c
JR
361static int action_executor_stop_session_handler(
362 struct action_executor *executor,
f2b3ef9f 363 const struct action_work_item *work_item,
72365501 364 struct action_work_subitem *item)
f2b3ef9f
JG
365{
366 int ret = 0;
367 const char *session_name;
368 enum lttng_action_status action_status;
369 struct ltt_session *session;
370 enum lttng_error_code cmd_ret;
72365501 371 struct lttng_action *action = item->action;
f2b3ef9f
JG
372
373 action_status = lttng_action_stop_session_get_session_name(
374 action, &session_name);
375 if (action_status != LTTNG_ACTION_STATUS_OK) {
376 ERR("Failed to get session name from `%s` action",
377 get_action_name(action));
378 ret = -1;
379 goto end;
380 }
381
72365501
JR
382 /*
383 * Validate if, at the moment the action was queued, the target session
384 * existed. If not, skip the action altogether.
385 */
386 if (!item->context.session_id.is_set) {
4ec6f5b6 387 DBG("Session `%s` was not present at the moment the work item was enqueued for `%s` action of trigger `%s`",
72365501
JR
388 session_name, get_action_name(action),
389 get_trigger_name(work_item->trigger));
390 lttng_action_increase_execution_failure_count(action);
72365501
JR
391 goto end;
392 }
393
f2b3ef9f 394 session_lock_list();
4a467a84 395 session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
f2b3ef9f 396 if (!session) {
34f87583 397 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
f2b3ef9f 398 session_name, get_action_name(action),
34f87583 399 get_trigger_name(work_item->trigger));
2d57482c 400 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
401 goto error_unlock_list;
402 }
403
4a467a84
JR
404 session_lock(session);
405 if (session->destroyed) {
4ec6f5b6 406 DBG("Session `%s` with id = %" PRIu64 " is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
4a467a84 407 session->name, session->id,
72365501
JR
408 get_action_name(action),
409 get_trigger_name(work_item->trigger));
4a467a84 410 goto error_unlock_session;
72365501
JR
411 }
412
f2b3ef9f 413 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
635e0160 414 goto error_unlock_session;
f2b3ef9f
JG
415 }
416
7966af57 417 cmd_ret = (lttng_error_code) cmd_stop_trace(session);
f2b3ef9f
JG
418 switch (cmd_ret) {
419 case LTTNG_OK:
34f87583
JR
420 DBG("Successfully stopped session `%s` on behalf of trigger `%s`",
421 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
422 break;
423 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
34f87583
JR
424 DBG("Attempted to stop session `%s` on behalf of trigger `%s` but it was already stopped",
425 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
426 break;
427 default:
34f87583
JR
428 WARN("Failed to stop session `%s` on behalf of trigger `%s`: %s",
429 session_name, get_trigger_name(work_item->trigger),
f2b3ef9f 430 lttng_strerror(-cmd_ret));
2d57482c 431 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
432 break;
433 }
434
635e0160 435error_unlock_session:
f2b3ef9f
JG
436 session_unlock(session);
437 session_put(session);
438error_unlock_list:
439 session_unlock_list();
440end:
441 return ret;
442}
443
2d57482c
JR
444static int action_executor_rotate_session_handler(
445 struct action_executor *executor,
f2b3ef9f 446 const struct action_work_item *work_item,
72365501 447 struct action_work_subitem *item)
f2b3ef9f
JG
448{
449 int ret = 0;
450 const char *session_name;
451 enum lttng_action_status action_status;
452 struct ltt_session *session;
453 enum lttng_error_code cmd_ret;
72365501 454 struct lttng_action *action = item->action;
f2b3ef9f
JG
455
456 action_status = lttng_action_rotate_session_get_session_name(
457 action, &session_name);
458 if (action_status != LTTNG_ACTION_STATUS_OK) {
459 ERR("Failed to get session name from `%s` action",
460 get_action_name(action));
461 ret = -1;
462 goto end;
463 }
464
72365501
JR
465 /*
466 * Validate if, at the moment the action was queued, the target session
467 * existed. If not, skip the action altogether.
468 */
469 if (!item->context.session_id.is_set) {
4ec6f5b6 470 DBG("Session `%s` was not present at the moment the work item was enqueued for `%s` action of trigger `%s`",
72365501
JR
471 session_name, get_action_name(action),
472 get_trigger_name(work_item->trigger));
473 lttng_action_increase_execution_failure_count(action);
72365501
JR
474 goto end;
475 }
476
f2b3ef9f 477 session_lock_list();
4a467a84 478 session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
f2b3ef9f 479 if (!session) {
34f87583 480 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
f2b3ef9f 481 session_name, get_action_name(action),
34f87583 482 get_trigger_name(work_item->trigger));
2d57482c 483 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
484 goto error_unlock_list;
485 }
486
4a467a84
JR
487 session_lock(session);
488 if (session->destroyed) {
4ec6f5b6 489 DBG("Session `%s` with id = %" PRIu64 " is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
4a467a84 490 session->name, session->id,
72365501
JR
491 get_action_name(action),
492 get_trigger_name(work_item->trigger));
4a467a84 493 goto error_unlock_session;
72365501
JR
494 }
495
f2b3ef9f 496 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
635e0160 497 goto error_unlock_session;
f2b3ef9f
JG
498 }
499
7966af57 500 cmd_ret = (lttng_error_code) cmd_rotate_session(session, NULL, false,
f2b3ef9f
JG
501 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
502 switch (cmd_ret) {
503 case LTTNG_OK:
34f87583
JR
504 DBG("Successfully started rotation of session `%s` on behalf of trigger `%s`",
505 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
506 break;
507 case LTTNG_ERR_ROTATION_PENDING:
34f87583
JR
508 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%s` but a rotation is already ongoing",
509 session_name, get_trigger_name(work_item->trigger));
2d57482c 510 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
511 break;
512 case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP:
513 case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR:
34f87583
JR
514 DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%s` but a rotation has already been completed since the last stop or clear",
515 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
516 break;
517 default:
34f87583
JR
518 WARN("Failed to start a rotation of session `%s` on behalf of trigger `%s`: %s",
519 session_name, get_trigger_name(work_item->trigger),
f2b3ef9f 520 lttng_strerror(-cmd_ret));
2d57482c 521 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
522 break;
523 }
524
635e0160 525error_unlock_session:
f2b3ef9f
JG
526 session_unlock(session);
527 session_put(session);
528error_unlock_list:
529 session_unlock_list();
530end:
531 return ret;
532}
533
2d57482c
JR
534static int action_executor_snapshot_session_handler(
535 struct action_executor *executor,
f2b3ef9f 536 const struct action_work_item *work_item,
72365501 537 struct action_work_subitem *item)
f2b3ef9f
JG
538{
539 int ret = 0;
540 const char *session_name;
541 enum lttng_action_status action_status;
542 struct ltt_session *session;
7966af57 543 lttng_snapshot_output default_snapshot_output;
f2b3ef9f
JG
544 const struct lttng_snapshot_output *snapshot_output =
545 &default_snapshot_output;
546 enum lttng_error_code cmd_ret;
72365501
JR
547 struct lttng_action *action = item->action;
548
7966af57
SM
549 default_snapshot_output.max_size = UINT64_MAX;
550
72365501
JR
551 /*
552 * Validate if, at the moment the action was queued, the target session
553 * existed. If not, skip the action altogether.
554 */
555 if (!item->context.session_id.is_set) {
4ec6f5b6 556 DBG("Session was not present at the moment the work item was enqueued for `%s` action of trigger `%s`",
42ef691e 557 get_action_name(action),
72365501
JR
558 get_trigger_name(work_item->trigger));
559 lttng_action_increase_execution_failure_count(action);
72365501
JR
560 goto end;
561 }
f2b3ef9f
JG
562
563 action_status = lttng_action_snapshot_session_get_session_name(
564 action, &session_name);
565 if (action_status != LTTNG_ACTION_STATUS_OK) {
566 ERR("Failed to get session name from `%s` action",
567 get_action_name(action));
568 ret = -1;
569 goto end;
570 }
571
572 action_status = lttng_action_snapshot_session_get_output(
573 action, &snapshot_output);
574 if (action_status != LTTNG_ACTION_STATUS_OK &&
575 action_status != LTTNG_ACTION_STATUS_UNSET) {
576 ERR("Failed to get output from `%s` action",
577 get_action_name(action));
578 ret = -1;
579 goto end;
580 }
581
582 session_lock_list();
4a467a84 583 session = session_find_by_id(LTTNG_OPTIONAL_GET(item->context.session_id));
f2b3ef9f 584 if (!session) {
ca46af4e 585 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`",
f2b3ef9f 586 session_name, get_action_name(action),
ca46af4e 587 get_trigger_name(work_item->trigger));
2d57482c 588 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
589 goto error_unlock_list;
590 }
591
4a467a84
JR
592 session_lock(session);
593 if (session->destroyed) {
4ec6f5b6 594 DBG("Session `%s` with id = %" PRIu64 " is flagged as destroyed. Skipping: action = `%s`, trigger = `%s`",
4a467a84 595 session->name, session->id,
72365501
JR
596 get_action_name(action),
597 get_trigger_name(work_item->trigger));
4a467a84 598 goto error_unlock_session;
72365501 599 }
f2b3ef9f 600
f2b3ef9f 601 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
635e0160 602 goto error_unlock_session;
f2b3ef9f
JG
603 }
604
7966af57 605 cmd_ret = (lttng_error_code) cmd_snapshot_record(session, snapshot_output, 0);
f2b3ef9f
JG
606 switch (cmd_ret) {
607 case LTTNG_OK:
34f87583
JR
608 DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%s`",
609 session_name, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
610 break;
611 default:
34f87583
JR
612 WARN("Failed to record snapshot of session `%s` on behalf of trigger `%s`: %s",
613 session_name, get_trigger_name(work_item->trigger),
f2b3ef9f 614 lttng_strerror(-cmd_ret));
2d57482c 615 lttng_action_increase_execution_failure_count(action);
f2b3ef9f
JG
616 break;
617 }
618
635e0160 619error_unlock_session:
f2b3ef9f
JG
620 session_unlock(session);
621 session_put(session);
622error_unlock_list:
623 session_unlock_list();
624end:
625 return ret;
626}
627
7c2fae7c 628static int action_executor_list_handler(struct action_executor *executor,
f2b3ef9f 629 const struct action_work_item *work_item,
72365501 630 struct action_work_subitem *item)
f2b3ef9f 631{
7c2fae7c 632 ERR("Execution of a list action by the action executor should never occur");
72365501 633 abort();
f2b3ef9f
JG
634}
635
636static int action_executor_generic_handler(struct action_executor *executor,
637 const struct action_work_item *work_item,
72365501 638 struct action_work_subitem *item)
f2b3ef9f 639{
2d57482c 640 int ret;
72365501 641 struct lttng_action *action = item->action;
0e43bcbf
JG
642 const enum lttng_action_type action_type = lttng_action_get_type(action);
643
a0377dfe 644 LTTNG_ASSERT(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
0e43bcbf 645
2d57482c
JR
646 lttng_action_increase_execution_request_count(action);
647 if (!lttng_action_should_execute(action)) {
648 DBG("Policy prevented execution of action `%s` of trigger `%s` action work item %" PRIu64,
649 get_action_name(action),
650 get_trigger_name(work_item->trigger),
651 work_item->id);
652 ret = 0;
653 goto end;
654 }
655
656 lttng_action_increase_execution_count(action);
2516f2d8 657 DBG("Executing action `%s` of trigger `%s` action work item %" PRIu64,
f2b3ef9f 658 get_action_name(action),
34f87583 659 get_trigger_name(work_item->trigger),
f2b3ef9f 660 work_item->id);
72365501 661 ret = action_executors[action_type](executor, work_item, item);
2d57482c
JR
662end:
663 return ret;
f2b3ef9f
JG
664}
665
666static int action_work_item_execute(struct action_executor *executor,
667 struct action_work_item *work_item)
668{
669 int ret;
72365501 670 size_t count, i;
f2b3ef9f 671
34f87583
JR
672 DBG("Starting execution of action work item %" PRIu64 " of trigger `%s`",
673 work_item->id, get_trigger_name(work_item->trigger));
72365501 674
be65f802 675 count = lttng_dynamic_array_get_count(&work_item->subitems);
72365501
JR
676 for (i = 0; i < count; i++) {
677 struct action_work_subitem *item;
678
7966af57 679 item = (action_work_subitem *) lttng_dynamic_array_get_element(&work_item->subitems, i);
72365501
JR
680 ret = action_executor_generic_handler(
681 executor, work_item, item);
682 if (ret) {
683 goto end;
684 }
685 }
686end:
34f87583
JR
687 DBG("Completed execution of action work item %" PRIu64 " of trigger `%s`",
688 work_item->id, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
689 return ret;
690}
691
692static void action_work_item_destroy(struct action_work_item *work_item)
693{
694 lttng_trigger_put(work_item->trigger);
695 lttng_evaluation_destroy(work_item->evaluation);
696 notification_client_list_put(work_item->client_list);
be65f802 697 lttng_dynamic_array_reset(&work_item->subitems);
f2b3ef9f
JG
698 free(work_item);
699}
700
701static void *action_executor_thread(void *_data)
702{
7966af57 703 struct action_executor *executor = (action_executor *) _data;
f2b3ef9f 704
a0377dfe 705 LTTNG_ASSERT(executor);
f2b3ef9f 706
412d7227
SM
707 health_register(the_health_sessiond,
708 HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR);
f2b3ef9f
JG
709
710 rcu_register_thread();
711 rcu_thread_online();
712
713 DBG("Entering work execution loop");
714 pthread_mutex_lock(&executor->work.lock);
715 while (!executor->should_quit) {
da247508 716 int ret = 0;
f2b3ef9f
JG
717 struct action_work_item *work_item;
718
719 health_code_update();
720 if (executor->work.pending_count == 0) {
721 health_poll_entry();
722 DBG("No work items enqueued, entering wait");
723 pthread_cond_wait(&executor->work.cond,
724 &executor->work.lock);
725 DBG("Woke-up from wait");
726 health_poll_exit();
727 continue;
728 }
729
0db0f8e0 730 /* Pop item from front of the list with work lock held. */
f2b3ef9f
JG
731 work_item = cds_list_first_entry(&executor->work.list,
732 struct action_work_item, list_node);
733 cds_list_del(&work_item->list_node);
734 executor->work.pending_count--;
735
736 /*
737 * Work can be performed without holding the work lock,
738 * allowing new items to be queued.
739 */
740 pthread_mutex_unlock(&executor->work.lock);
d2a28b27
JR
741
742 /* Execute item only if a trigger is registered. */
743 lttng_trigger_lock(work_item->trigger);
744 if (!lttng_trigger_is_registered(work_item->trigger)) {
745 const char *trigger_name = NULL;
746 uid_t trigger_owner_uid;
747 enum lttng_trigger_status trigger_status;
748
0efb2ad7 749 trigger_name = get_trigger_name(work_item->trigger);
d2a28b27
JR
750
751 trigger_status = lttng_trigger_get_owner_uid(
752 work_item->trigger, &trigger_owner_uid);
a0377dfe 753 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
d2a28b27 754
4ec6f5b6 755 DBG("Work item skipped since the associated trigger is no longer registered: work item id = %" PRIu64 ", trigger name = `%s`, trigger owner uid = %d",
d2a28b27
JR
756 work_item->id, trigger_name,
757 (int) trigger_owner_uid);
758 ret = 0;
759 goto skip_execute;
760 }
761
f2b3ef9f 762 ret = action_work_item_execute(executor, work_item);
d2a28b27
JR
763
764 skip_execute:
765 lttng_trigger_unlock(work_item->trigger);
f2b3ef9f
JG
766 action_work_item_destroy(work_item);
767 if (ret) {
768 /* Fatal error. */
769 break;
770 }
771
772 health_code_update();
773 pthread_mutex_lock(&executor->work.lock);
774 }
775
f5f5c54d
JG
776 if (executor->should_quit) {
777 pthread_mutex_unlock(&executor->work.lock);
778 }
f2b3ef9f
JG
779 DBG("Left work execution loop");
780
781 health_code_update();
782
783 rcu_thread_offline();
784 rcu_unregister_thread();
412d7227 785 health_unregister(the_health_sessiond);
f2b3ef9f
JG
786
787 return NULL;
788}
789
790static bool shutdown_action_executor_thread(void *_data)
791{
7966af57 792 struct action_executor *executor = (action_executor *) _data;
f2b3ef9f 793
8db3acaf 794 pthread_mutex_lock(&executor->work.lock);
f2b3ef9f
JG
795 executor->should_quit = true;
796 pthread_cond_signal(&executor->work.cond);
8db3acaf 797 pthread_mutex_unlock(&executor->work.lock);
f2b3ef9f
JG
798 return true;
799}
800
801static void clean_up_action_executor_thread(void *_data)
802{
7966af57 803 struct action_executor *executor = (action_executor *) _data;
f2b3ef9f 804
a0377dfe 805 LTTNG_ASSERT(cds_list_empty(&executor->work.list));
f2b3ef9f
JG
806
807 pthread_mutex_destroy(&executor->work.lock);
808 pthread_cond_destroy(&executor->work.cond);
809 free(executor);
810}
811
812struct action_executor *action_executor_create(
813 struct notification_thread_handle *handle)
814{
7966af57 815 struct action_executor *executor = (action_executor *) zmalloc(sizeof(*executor));
f2b3ef9f
JG
816
817 if (!executor) {
818 goto end;
819 }
820
821 CDS_INIT_LIST_HEAD(&executor->work.list);
822 pthread_cond_init(&executor->work.cond, NULL);
823 pthread_mutex_init(&executor->work.lock, NULL);
824 executor->notification_thread_handle = handle;
825
826 executor->thread = lttng_thread_create(THREAD_NAME,
827 action_executor_thread, shutdown_action_executor_thread,
828 clean_up_action_executor_thread, executor);
829end:
830 return executor;
831}
832
833void action_executor_destroy(struct action_executor *executor)
834{
835 struct action_work_item *work_item, *tmp;
836
837 /* TODO Wait for work list to drain? */
838 lttng_thread_shutdown(executor->thread);
839 pthread_mutex_lock(&executor->work.lock);
840 if (executor->work.pending_count != 0) {
841 WARN("%" PRIu64
842 " trigger action%s still queued for execution and will be discarded",
843 executor->work.pending_count,
844 executor->work.pending_count == 1 ? " is" :
845 "s are");
846 }
847
848 cds_list_for_each_entry_safe (
849 work_item, tmp, &executor->work.list, list_node) {
850 WARN("Discarding action work item %" PRIu64
34f87583
JR
851 " associated to trigger `%s`",
852 work_item->id, get_trigger_name(work_item->trigger));
f2b3ef9f
JG
853 cds_list_del(&work_item->list_node);
854 action_work_item_destroy(work_item);
855 }
856 pthread_mutex_unlock(&executor->work.lock);
857 lttng_thread_put(executor->thread);
858}
859
860/* RCU read-lock must be held by the caller. */
72365501 861enum action_executor_status action_executor_enqueue_trigger(
f2b3ef9f
JG
862 struct action_executor *executor,
863 struct lttng_trigger *trigger,
864 struct lttng_evaluation *evaluation,
865 const struct lttng_credentials *object_creds,
866 struct notification_client_list *client_list)
867{
72365501 868 int ret;
f2b3ef9f
JG
869 enum action_executor_status executor_status = ACTION_EXECUTOR_STATUS_OK;
870 const uint64_t work_item_id = executor->next_work_item_id++;
871 struct action_work_item *work_item;
872 bool signal = false;
72365501 873
a0377dfe 874 LTTNG_ASSERT(trigger);
72365501 875
f2b3ef9f
JG
876 pthread_mutex_lock(&executor->work.lock);
877 /* Check for queue overflow. */
878 if (executor->work.pending_count >= MAX_QUEUED_WORK_COUNT) {
879 /* Most likely spammy, remove if it is the case. */
72365501
JR
880 DBG("Refusing to enqueue action for trigger (overflow): trigger name = `%s`, work item id = %" PRIu64,
881 get_trigger_name(trigger), work_item_id);
f2b3ef9f
JG
882 executor_status = ACTION_EXECUTOR_STATUS_OVERFLOW;
883 goto error_unlock;
884 }
885
7966af57 886 work_item = (action_work_item *) zmalloc(sizeof(*work_item));
f2b3ef9f 887 if (!work_item) {
4ec6f5b6 888 PERROR("Failed to allocate action executor work item: trigger name = `%s`",
34f87583 889 get_trigger_name(trigger));
f2b3ef9f
JG
890 executor_status = ACTION_EXECUTOR_STATUS_ERROR;
891 goto error_unlock;
892 }
893
894 lttng_trigger_get(trigger);
895 if (client_list) {
896 const bool reference_acquired =
897 notification_client_list_get(client_list);
898
a0377dfe 899 LTTNG_ASSERT(reference_acquired);
f2b3ef9f
JG
900 }
901
7966af57
SM
902 work_item->id = work_item_id;
903 work_item->trigger = trigger;
f2b3ef9f 904
7966af57
SM
905 /* Ownership transferred to the work item. */
906 work_item->evaluation = evaluation;
f2b3ef9f 907 evaluation = NULL;
be65f802 908
7966af57
SM
909 work_item->client_list = client_list;
910 work_item->object_creds.is_set = !!object_creds;
911 if (object_creds) {
912 work_item->object_creds.value = *object_creds;
913 }
914
915 CDS_INIT_LIST_HEAD(&work_item->list_node);
916
be65f802
JG
917 /* Build the array of action work subitems for the passed trigger. */
918 lttng_dynamic_array_init(&work_item->subitems,
919 sizeof(struct action_work_subitem),
920 action_work_subitem_destructor);
921
922 ret = populate_subitem_array_from_trigger(
923 trigger, &work_item->subitems);
924 if (ret) {
925 ERR("Failed to populate work item sub items on behalf of trigger: trigger name = `%s`",
926 get_trigger_name(trigger));
927 executor_status = ACTION_EXECUTOR_STATUS_ERROR;
928 goto error_unlock;
929 }
930
f2b3ef9f
JG
931 cds_list_add_tail(&work_item->list_node, &executor->work.list);
932 executor->work.pending_count++;
72365501 933 DBG("Enqueued action for trigger: trigger name = `%s`, work item id = %" PRIu64,
34f87583 934 get_trigger_name(trigger), work_item_id);
f2b3ef9f
JG
935 signal = true;
936
937error_unlock:
f2b3ef9f
JG
938 if (signal) {
939 pthread_cond_signal(&executor->work.cond);
940 }
941
be65f802 942 pthread_mutex_unlock(&executor->work.lock);
f2b3ef9f
JG
943 lttng_evaluation_destroy(evaluation);
944 return executor_status;
945}
72365501
JR
946
947static int add_action_to_subitem_array(struct lttng_action *action,
948 struct lttng_dynamic_array *subitems)
949{
da247508 950 int ret = 0;
72365501
JR
951 enum lttng_action_type type = lttng_action_get_type(action);
952 const char *session_name = NULL;
953 enum lttng_action_status status;
954 struct action_work_subitem subitem = {
955 .action = NULL,
956 .context = {
957 .session_id = LTTNG_OPTIONAL_INIT_UNSET,
958 },
959 };
960
a0377dfe
FD
961 LTTNG_ASSERT(action);
962 LTTNG_ASSERT(subitems);
72365501 963
7c2fae7c 964 if (type == LTTNG_ACTION_TYPE_LIST) {
72365501
JR
965 unsigned int count, i;
966
702f26c8 967 status = lttng_action_list_get_count(action, &count);
a0377dfe 968 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
72365501
JR
969
970 for (i = 0; i < count; i++) {
971 struct lttng_action *inner_action = NULL;
972
702f26c8 973 inner_action = lttng_action_list_borrow_mutable_at_index(
72365501 974 action, i);
a0377dfe 975 LTTNG_ASSERT(inner_action);
72365501
JR
976 ret = add_action_to_subitem_array(
977 inner_action, subitems);
978 if (ret) {
979 goto end;
980 }
981 }
982
983 /*
984 * Go directly to the end since there is no need to add the
7c2fae7c 985 * list action by itself to the subitems array.
72365501
JR
986 */
987 goto end;
988 }
989
990 /* Gather execution context. */
991 switch (type) {
992 case LTTNG_ACTION_TYPE_NOTIFY:
993 break;
994 case LTTNG_ACTION_TYPE_START_SESSION:
995 status = lttng_action_start_session_get_session_name(
996 action, &session_name);
a0377dfe 997 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
72365501
JR
998 break;
999 case LTTNG_ACTION_TYPE_STOP_SESSION:
1000 status = lttng_action_stop_session_get_session_name(
1001 action, &session_name);
a0377dfe 1002 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
72365501
JR
1003 break;
1004 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
1005 status = lttng_action_rotate_session_get_session_name(
1006 action, &session_name);
a0377dfe 1007 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
72365501
JR
1008 break;
1009 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
1010 status = lttng_action_snapshot_session_get_session_name(
1011 action, &session_name);
a0377dfe 1012 LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
72365501 1013 break;
7c2fae7c 1014 case LTTNG_ACTION_TYPE_LIST:
72365501
JR
1015 case LTTNG_ACTION_TYPE_UNKNOWN:
1016 /* Fallthrough */
1017 default:
1018 abort();
1019 break;
1020 }
1021
1022 /*
1023 * Fetch the session execution context info as needed.
1024 * Note that we could decide to not add an action for which we know the
1025 * execution will not happen (i.e no session exists for that name). For
1026 * now we leave the decision to skip to the action executor for sake of
1027 * simplicity and consistency.
1028 */
1029 if (session_name != NULL) {
e1bbf989 1030 uint64_t session_id;
72365501 1031
e1bbf989
JR
1032 /*
1033 * Instantaneous sampling of the session id if present.
1034 *
1035 * This method is preferred over `sessiond_find_by_name` then
1036 * fetching the session'd id since `sessiond_find_by_name`
1037 * requires the session list lock to be taken.
1038 *
1039 * Taking the session list lock can lead to a deadlock
1040 * between the action executor and the notification thread
1041 * (caller of add_action_to_subitem_array). It is okay if the
1042 * session state changes between the enqueuing time and the
1043 * execution time. The execution context is validated at
1044 * execution time.
1045 */
1046 if (sample_session_id_by_name(session_name, &session_id)) {
72365501 1047 LTTNG_OPTIONAL_SET(&subitem.context.session_id,
e1bbf989 1048 session_id);
72365501 1049 }
72365501
JR
1050 }
1051
1052 /* Get a reference to the action. */
1053 lttng_action_get(action);
1054 subitem.action = action;
1055
1056 ret = lttng_dynamic_array_add_element(subitems, &subitem);
1057 if (ret) {
1058 ERR("Failed to add work subitem to the subitem array");
1059 lttng_action_put(action);
1060 ret = -1;
1061 goto end;
1062 }
1063
1064end:
1065 return ret;
1066}
1067
1068static int populate_subitem_array_from_trigger(struct lttng_trigger *trigger,
1069 struct lttng_dynamic_array *subitems)
1070{
1071 struct lttng_action *action;
1072
1073 action = lttng_trigger_get_action(trigger);
a0377dfe 1074 LTTNG_ASSERT(action);
72365501
JR
1075
1076 return add_action_to_subitem_array(action, subitems);
1077}
This page took 0.08409 seconds and 4 git commands to generate.