trigger: expose trigger owner uid
[lttng-tools.git] / src / bin / lttng-sessiond / action-executor.c
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"
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>
27 #include <pthread.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <urcu/list.h>
31
32 #define THREAD_NAME "Action Executor"
33 #define MAX_QUEUED_WORK_COUNT 8192
34
35 struct action_work_item {
36 uint64_t id;
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;
42 };
43
44 struct action_executor {
45 struct lttng_thread *thread;
46 struct notification_thread_handle *notification_thread_handle;
47 struct {
48 uint64_t pending_count;
49 struct cds_list_head list;
50 pthread_cond_t cond;
51 pthread_mutex_t lock;
52 } work;
53 bool should_quit;
54 uint64_t next_work_item_id;
55 };
56
57 /*
58 * Only return non-zero on a fatal error that should shut down the action
59 * executor.
60 */
61 typedef int (*action_executor_handler)(struct action_executor *executor,
62 const struct action_work_item *,
63 const struct lttng_action *action);
64
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 *);
86
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,
94 };
95
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",
103 };
104
105 static const char *get_action_name(const struct lttng_action *action)
106 {
107 const enum lttng_action_type action_type = lttng_action_get_type(action);
108
109 assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
110
111 return action_type_names[action_type];
112 }
113
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)
117 {
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),
122 };
123 /* Can never be NULL. */
124 const struct lttng_credentials *trigger_creds =
125 lttng_trigger_get_credentials(trigger);
126
127 is_allowed = (lttng_credentials_is_equal_uid(trigger_creds, &session_creds)) ||
128 (lttng_credentials_get_uid(trigger_creds) == 0);
129 if (!is_allowed) {
130 WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld",
131 session->name,
132 (long int) session->uid,
133 (long int) session->gid,
134 (long int) lttng_credentials_get_uid(trigger_creds));
135 }
136
137 return is_allowed;
138 }
139
140 static int client_handle_transmission_status(
141 struct notification_client *client,
142 enum client_transmission_status status,
143 void *user_data)
144 {
145 int ret = 0;
146 struct action_executor *executor = user_data;
147 bool update_communication = true;
148
149 switch (status) {
150 case CLIENT_TRANSMISSION_STATUS_COMPLETE:
151 DBG("Successfully sent full notification to client, client_id = %" PRIu64,
152 client->id);
153 update_communication = false;
154 break;
155 case CLIENT_TRANSMISSION_STATUS_QUEUED:
156 DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64,
157 client->id);
158 break;
159 case CLIENT_TRANSMISSION_STATUS_FAIL:
160 DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64,
161 client->id);
162 break;
163 default:
164 ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64,
165 client->id);
166 ret = -1;
167 goto end;
168 }
169
170 if (!update_communication) {
171 goto end;
172 }
173
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,
177 status);
178 end:
179 return ret;
180 }
181
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)
185 {
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);
192 }
193
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)
197 {
198 int ret = 0;
199 const char *session_name;
200 enum lttng_action_status action_status;
201 struct ltt_session *session;
202 enum lttng_error_code cmd_ret;
203
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));
209 ret = -1;
210 goto end;
211 }
212
213 session_lock_list();
214 session = session_find_by_name(session_name);
215 if (!session) {
216 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
217 session_name, get_action_name(action),
218 work_item->trigger);
219 goto error_unlock_list;
220 }
221
222 session_lock(session);
223 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
224 goto error_dispose_session;
225 }
226
227 cmd_ret = cmd_start_trace(session);
228 switch (cmd_ret) {
229 case LTTNG_OK:
230 DBG("Successfully started session `%s` on behalf of trigger `%p`",
231 session_name, work_item->trigger);
232 break;
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);
236 break;
237 default:
238 WARN("Failed to start session `%s` on behalf of trigger `%p`: %s",
239 session_name, work_item->trigger,
240 lttng_strerror(-cmd_ret));
241 break;
242 }
243
244 error_dispose_session:
245 session_unlock(session);
246 session_put(session);
247 error_unlock_list:
248 session_unlock_list();
249 end:
250 return ret;
251 }
252
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)
256 {
257 int ret = 0;
258 const char *session_name;
259 enum lttng_action_status action_status;
260 struct ltt_session *session;
261 enum lttng_error_code cmd_ret;
262
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));
268 ret = -1;
269 goto end;
270 }
271
272 session_lock_list();
273 session = session_find_by_name(session_name);
274 if (!session) {
275 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
276 session_name, get_action_name(action),
277 work_item->trigger);
278 goto error_unlock_list;
279 }
280
281 session_lock(session);
282 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
283 goto error_dispose_session;
284 }
285
286 cmd_ret = cmd_stop_trace(session);
287 switch (cmd_ret) {
288 case LTTNG_OK:
289 DBG("Successfully stopped session `%s` on behalf of trigger `%p`",
290 session_name, work_item->trigger);
291 break;
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);
295 break;
296 default:
297 WARN("Failed to stop session `%s` on behalf of trigger `%p`: %s",
298 session_name, work_item->trigger,
299 lttng_strerror(-cmd_ret));
300 break;
301 }
302
303 error_dispose_session:
304 session_unlock(session);
305 session_put(session);
306 error_unlock_list:
307 session_unlock_list();
308 end:
309 return ret;
310 }
311
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)
315 {
316 int ret = 0;
317 const char *session_name;
318 enum lttng_action_status action_status;
319 struct ltt_session *session;
320 enum lttng_error_code cmd_ret;
321
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));
327 ret = -1;
328 goto end;
329 }
330
331 session_lock_list();
332 session = session_find_by_name(session_name);
333 if (!session) {
334 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
335 session_name, get_action_name(action),
336 work_item->trigger);
337 goto error_unlock_list;
338 }
339
340 session_lock(session);
341 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
342 goto error_dispose_session;
343 }
344
345 cmd_ret = cmd_rotate_session(session, NULL, false,
346 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
347 switch (cmd_ret) {
348 case LTTNG_OK:
349 DBG("Successfully started rotation of session `%s` on behalf of trigger `%p`",
350 session_name, work_item->trigger);
351 break;
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);
355 break;
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);
360 break;
361 default:
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));
365 break;
366 }
367
368 error_dispose_session:
369 session_unlock(session);
370 session_put(session);
371 error_unlock_list:
372 session_unlock_list();
373 end:
374 return ret;
375 }
376
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)
380 {
381 int ret = 0;
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,
387 };
388 const struct lttng_snapshot_output *snapshot_output =
389 &default_snapshot_output;
390 enum lttng_error_code cmd_ret;
391
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));
397 ret = -1;
398 goto end;
399 }
400
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));
407 ret = -1;
408 goto end;
409 }
410
411 session_lock_list();
412 session = session_find_by_name(session_name);
413 if (!session) {
414 DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%p`",
415 session_name, get_action_name(action),
416 work_item->trigger);
417 goto error_unlock_list;
418 }
419
420
421 session_lock(session);
422 if (!is_trigger_allowed_for_session(work_item->trigger, session)) {
423 goto error_dispose_session;
424 }
425
426 cmd_ret = cmd_snapshot_record(session, snapshot_output, 0);
427 switch (cmd_ret) {
428 case LTTNG_OK:
429 DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%p`",
430 session_name, work_item->trigger);
431 break;
432 default:
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));
436 break;
437 }
438
439 error_dispose_session:
440 session_unlock(session);
441 session_put(session);
442 error_unlock_list:
443 session_unlock_list();
444 end:
445 return ret;
446 }
447
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)
451 {
452 int ret = 0;
453 unsigned int i, count;
454 enum lttng_action_status action_status;
455
456 action_status = lttng_action_group_get_count(action_group, &count);
457 if (action_status != LTTNG_ACTION_STATUS_OK) {
458 /* Fatal error. */
459 ERR("Failed to get count of action in action group");
460 ret = -1;
461 goto end;
462 }
463
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(
468 action_group, i);
469
470 ret = action_executor_generic_handler(
471 executor, work_item, action);
472 if (ret) {
473 ERR("Stopping the execution of the action group of trigger `%p` following a fatal error",
474 work_item->trigger);
475 goto end;
476 }
477 }
478 end:
479 return ret;
480 }
481
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)
485 {
486 const enum lttng_action_type action_type = lttng_action_get_type(action);
487
488 assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
489
490 DBG("Executing action `%s` of trigger `%p` action work item %" PRIu64,
491 get_action_name(action),
492 work_item->trigger,
493 work_item->id);
494
495 return action_executors[action_type](
496 executor, work_item, action);
497 }
498
499 static int action_work_item_execute(struct action_executor *executor,
500 struct action_work_item *work_item)
501 {
502 int ret;
503 const struct lttng_action *action =
504 lttng_trigger_get_const_action(work_item->trigger);
505
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);
511 return ret;
512 }
513
514 static void action_work_item_destroy(struct action_work_item *work_item)
515 {
516 lttng_trigger_put(work_item->trigger);
517 lttng_evaluation_destroy(work_item->evaluation);
518 notification_client_list_put(work_item->client_list);
519 free(work_item);
520 }
521
522 static void *action_executor_thread(void *_data)
523 {
524 struct action_executor *executor = _data;
525
526 assert(executor);
527
528 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR);
529
530 rcu_register_thread();
531 rcu_thread_online();
532
533 DBG("Entering work execution loop");
534 pthread_mutex_lock(&executor->work.lock);
535 while (!executor->should_quit) {
536 int ret;
537 struct action_work_item *work_item;
538
539 health_code_update();
540 if (executor->work.pending_count == 0) {
541 health_poll_entry();
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");
546 health_poll_exit();
547 continue;
548 }
549
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--;
555
556 /*
557 * Work can be performed without holding the work lock,
558 * allowing new items to be queued.
559 */
560 pthread_mutex_unlock(&executor->work.lock);
561 ret = action_work_item_execute(executor, work_item);
562 action_work_item_destroy(work_item);
563 if (ret) {
564 /* Fatal error. */
565 break;
566 }
567
568 health_code_update();
569 pthread_mutex_lock(&executor->work.lock);
570 }
571
572 if (executor->should_quit) {
573 pthread_mutex_unlock(&executor->work.lock);
574 }
575 DBG("Left work execution loop");
576
577 health_code_update();
578
579 rcu_thread_offline();
580 rcu_unregister_thread();
581 health_unregister(health_sessiond);
582
583 return NULL;
584 }
585
586 static bool shutdown_action_executor_thread(void *_data)
587 {
588 struct action_executor *executor = _data;
589
590 executor->should_quit = true;
591 pthread_cond_signal(&executor->work.cond);
592 return true;
593 }
594
595 static void clean_up_action_executor_thread(void *_data)
596 {
597 struct action_executor *executor = _data;
598
599 assert(cds_list_empty(&executor->work.list));
600
601 pthread_mutex_destroy(&executor->work.lock);
602 pthread_cond_destroy(&executor->work.cond);
603 free(executor);
604 }
605
606 struct action_executor *action_executor_create(
607 struct notification_thread_handle *handle)
608 {
609 struct action_executor *executor = zmalloc(sizeof(*executor));
610
611 if (!executor) {
612 goto end;
613 }
614
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;
619
620 executor->thread = lttng_thread_create(THREAD_NAME,
621 action_executor_thread, shutdown_action_executor_thread,
622 clean_up_action_executor_thread, executor);
623 end:
624 return executor;
625 }
626
627 void action_executor_destroy(struct action_executor *executor)
628 {
629 struct action_work_item *work_item, *tmp;
630
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) {
635 WARN("%" PRIu64
636 " trigger action%s still queued for execution and will be discarded",
637 executor->work.pending_count,
638 executor->work.pending_count == 1 ? " is" :
639 "s are");
640 }
641
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);
649 }
650 pthread_mutex_unlock(&executor->work.lock);
651 lttng_thread_put(executor->thread);
652 }
653
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)
661 {
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;
665 bool signal = false;
666
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;
674 goto error_unlock;
675 }
676
677 work_item = zmalloc(sizeof(*work_item));
678 if (!work_item) {
679 PERROR("Failed to allocate action executor work item on behalf of trigger `%p`",
680 trigger);
681 executor_status = ACTION_EXECUTOR_STATUS_ERROR;
682 goto error_unlock;
683 }
684
685 lttng_trigger_get(trigger);
686 if (client_list) {
687 const bool reference_acquired =
688 notification_client_list_get(client_list);
689
690 assert(reference_acquired);
691 }
692
693 *work_item = (typeof(*work_item)){
694 .id = work_item_id,
695 .trigger = trigger,
696 /* Ownership transferred to the work item. */
697 .evaluation = evaluation,
698 .object_creds = {
699 .is_set = !!object_creds,
700 .value = object_creds ? *object_creds :
701 (typeof(work_item->object_creds.value)) {},
702 },
703 .client_list = client_list,
704 .list_node = CDS_LIST_HEAD_INIT(work_item->list_node),
705 };
706
707 evaluation = NULL;
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);
712 signal = true;
713
714 error_unlock:
715 pthread_mutex_unlock(&executor->work.lock);
716 if (signal) {
717 pthread_cond_signal(&executor->work.cond);
718 }
719
720 lttng_evaluation_destroy(evaluation);
721 return executor_status;
722 }
This page took 0.043978 seconds and 5 git commands to generate.