cc857544390ea24674b8da75cf035fc59a0fd430
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.cpp
1 /*
2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include "cmd.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "notification-thread-commands.hpp"
14 #include "rotate.hpp"
15 #include "rotation-thread.hpp"
16 #include "session.hpp"
17 #include "utils.hpp"
18
19 #include <common/align.hpp>
20 #include <common/config/session-config.hpp>
21 #include <common/credentials.hpp>
22 #include <common/defaults.hpp>
23 #include <common/error.hpp>
24 #include <common/futex.hpp>
25 #include <common/hashtable/utils.hpp>
26 #include <common/kernel-ctl/kernel-ctl.hpp>
27 #include <common/time.hpp>
28 #include <common/utils.hpp>
29
30 #include <lttng/action/action-internal.hpp>
31 #include <lttng/condition/condition-internal.hpp>
32 #include <lttng/notification/channel-internal.hpp>
33 #include <lttng/rotate-internal.hpp>
34 #include <lttng/trigger/trigger.h>
35
36 #include <inttypes.h>
37 #include <signal.h>
38 #include <sys/stat.h>
39 #include <time.h>
40 #include <urcu.h>
41 #include <urcu/list.h>
42 #include <urcu/rculfhash.h>
43
44 int subscribe_session_consumed_size_rotation(
45 struct ltt_session *session,
46 uint64_t size,
47 struct notification_thread_handle *notification_thread_handle)
48 {
49 int ret;
50 enum lttng_condition_status condition_status;
51 enum lttng_notification_channel_status nc_status;
52 struct lttng_condition *rotate_condition = nullptr;
53 struct lttng_action *notify_action = nullptr;
54 const struct lttng_credentials session_creds = {
55 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
56 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
57 };
58
59 rotate_condition = lttng_condition_session_consumed_size_create();
60 if (!rotate_condition) {
61 ERR("Failed to create session consumed size condition object");
62 ret = -1;
63 goto end;
64 }
65
66 condition_status =
67 lttng_condition_session_consumed_size_set_threshold(rotate_condition, size);
68 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
69 ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
70 size);
71 ret = -1;
72 goto end;
73 }
74
75 condition_status = lttng_condition_session_consumed_size_set_session_name(rotate_condition,
76 session->name);
77 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
78 ERR("Could not set session consumed size condition session name (name = %s)",
79 session->name);
80 ret = -1;
81 goto end;
82 }
83
84 notify_action = lttng_action_notify_create();
85 if (!notify_action) {
86 ERR("Could not create notify action");
87 ret = -1;
88 goto end;
89 }
90
91 LTTNG_ASSERT(!session->rotate_trigger);
92 session->rotate_trigger = lttng_trigger_create(rotate_condition, notify_action);
93 if (!session->rotate_trigger) {
94 ERR("Could not create size-based rotation trigger");
95 ret = -1;
96 goto end;
97 }
98
99 /* Ensure this trigger is not visible to external users. */
100 lttng_trigger_set_hidden(session->rotate_trigger);
101 lttng_trigger_set_credentials(session->rotate_trigger, &session_creds);
102
103 nc_status =
104 lttng_notification_channel_subscribe(rotate_notification_channel, rotate_condition);
105 if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
106 ERR("Could not subscribe to session consumed size notification");
107 ret = -1;
108 goto end;
109 }
110
111 ret = notification_thread_command_register_trigger(
112 notification_thread_handle, session->rotate_trigger, true);
113 if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
114 ERR("Register trigger, %s", lttng_strerror(ret));
115 ret = -1;
116 goto end;
117 }
118
119 ret = 0;
120
121 end:
122 lttng_condition_put(rotate_condition);
123 lttng_action_put(notify_action);
124 if (ret) {
125 lttng_trigger_put(session->rotate_trigger);
126 }
127 return ret;
128 }
129
130 int unsubscribe_session_consumed_size_rotation(
131 struct ltt_session *session, struct notification_thread_handle *notification_thread_handle)
132 {
133 int ret = 0;
134 enum lttng_notification_channel_status status;
135
136 LTTNG_ASSERT(session->rotate_trigger);
137 status = lttng_notification_channel_unsubscribe(
138 rotate_notification_channel,
139 lttng_trigger_get_const_condition(session->rotate_trigger));
140 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
141 ERR("Session unsubscribe error: %d", (int) status);
142 ret = -1;
143 goto end;
144 }
145
146 ret = notification_thread_command_unregister_trigger(notification_thread_handle,
147 session->rotate_trigger);
148 if (ret != LTTNG_OK) {
149 ERR("Session unregister trigger error: %d", ret);
150 goto end;
151 }
152
153 lttng_trigger_put(session->rotate_trigger);
154 session->rotate_trigger = nullptr;
155
156 ret = 0;
157 end:
158 return ret;
159 }
This page took 0.033369 seconds and 3 git commands to generate.