Commit | Line | Data |
---|---|---|
db66e574 | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com> |
3 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
db66e574 | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
db66e574 | 6 | * |
db66e574 JD |
7 | */ |
8 | ||
9 | #define _LGPL_SOURCE | |
28ab034a JG |
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> | |
c9e313bc | 20 | #include <common/config/session-config.hpp> |
28ab034a | 21 | #include <common/credentials.hpp> |
c9e313bc | 22 | #include <common/defaults.hpp> |
28ab034a | 23 | #include <common/error.hpp> |
c9e313bc | 24 | #include <common/futex.hpp> |
c9e313bc SM |
25 | #include <common/hashtable/utils.hpp> |
26 | #include <common/kernel-ctl/kernel-ctl.hpp> | |
28ab034a JG |
27 | #include <common/time.hpp> |
28 | #include <common/utils.hpp> | |
db66e574 | 29 | |
28ab034a JG |
30 | #include <lttng/action/action-internal.hpp> |
31 | #include <lttng/condition/condition-internal.hpp> | |
c9e313bc SM |
32 | #include <lttng/notification/channel-internal.hpp> |
33 | #include <lttng/rotate-internal.hpp> | |
28ab034a | 34 | #include <lttng/trigger/trigger.h> |
db66e574 | 35 | |
28ab034a JG |
36 | #include <inttypes.h> |
37 | #include <signal.h> | |
38 | #include <sys/stat.h> | |
39 | #include <time.h> | |
db66e574 JD |
40 | #include <urcu.h> |
41 | #include <urcu/list.h> | |
42 | #include <urcu/rculfhash.h> | |
43 | ||
28ab034a JG |
44 | int subscribe_session_consumed_size_rotation( |
45 | struct ltt_session *session, | |
46 | uint64_t size, | |
47 | struct notification_thread_handle *notification_thread_handle) | |
90936dcf JD |
48 | { |
49 | int ret; | |
50 | enum lttng_condition_status condition_status; | |
51 | enum lttng_notification_channel_status nc_status; | |
dc65dda3 | 52 | const uint64_t eventfd_increment_value = 1; |
c08136a3 JG |
53 | struct lttng_condition *rotate_condition = nullptr; |
54 | struct lttng_action *notify_action = nullptr; | |
3da864a9 | 55 | const struct lttng_credentials session_creds = { |
ff588497 JR |
56 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid), |
57 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid), | |
3da864a9 | 58 | }; |
90936dcf | 59 | |
c08136a3 JG |
60 | rotate_condition = lttng_condition_session_consumed_size_create(); |
61 | if (!rotate_condition) { | |
90936dcf JD |
62 | ERR("Failed to create session consumed size condition object"); |
63 | ret = -1; | |
64 | goto end; | |
65 | } | |
66 | ||
28ab034a JG |
67 | condition_status = |
68 | lttng_condition_session_consumed_size_set_threshold(rotate_condition, size); | |
90936dcf JD |
69 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { |
70 | ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")", | |
28ab034a | 71 | size); |
90936dcf JD |
72 | ret = -1; |
73 | goto end; | |
74 | } | |
75 | ||
28ab034a JG |
76 | condition_status = lttng_condition_session_consumed_size_set_session_name(rotate_condition, |
77 | session->name); | |
90936dcf JD |
78 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { |
79 | ERR("Could not set session consumed size condition session name (name = %s)", | |
28ab034a | 80 | session->name); |
90936dcf JD |
81 | ret = -1; |
82 | goto end; | |
83 | } | |
84 | ||
c08136a3 JG |
85 | notify_action = lttng_action_notify_create(); |
86 | if (!notify_action) { | |
90936dcf JD |
87 | ERR("Could not create notify action"); |
88 | ret = -1; | |
89 | goto end; | |
90 | } | |
91 | ||
c08136a3 | 92 | LTTNG_ASSERT(!session->rotate_trigger); |
28ab034a | 93 | session->rotate_trigger = lttng_trigger_create(rotate_condition, notify_action); |
90936dcf JD |
94 | if (!session->rotate_trigger) { |
95 | ERR("Could not create size-based rotation trigger"); | |
96 | ret = -1; | |
97 | goto end; | |
98 | } | |
99 | ||
f2bda80e JG |
100 | /* Ensure this trigger is not visible to external users. */ |
101 | lttng_trigger_set_hidden(session->rotate_trigger); | |
28ab034a | 102 | lttng_trigger_set_credentials(session->rotate_trigger, &session_creds); |
3da864a9 | 103 | |
28ab034a JG |
104 | nc_status = |
105 | lttng_notification_channel_subscribe(rotate_notification_channel, rotate_condition); | |
90936dcf JD |
106 | if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { |
107 | ERR("Could not subscribe to session consumed size notification"); | |
108 | ret = -1; | |
109 | goto end; | |
110 | } | |
111 | ||
dc65dda3 JG |
112 | ret = lttng_write(rotate_notification_channel_subscription_change_eventfd, |
113 | &eventfd_increment_value, | |
114 | sizeof(eventfd_increment_value)); | |
115 | if (ret != sizeof(eventfd_increment_value)) { | |
116 | PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed"); | |
117 | ret = -1; | |
118 | goto end; | |
119 | } | |
120 | ||
90936dcf | 121 | ret = notification_thread_command_register_trigger( |
28ab034a | 122 | notification_thread_handle, session->rotate_trigger, true); |
90936dcf JD |
123 | if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) { |
124 | ERR("Register trigger, %s", lttng_strerror(ret)); | |
125 | ret = -1; | |
126 | goto end; | |
127 | } | |
128 | ||
129 | ret = 0; | |
130 | ||
131 | end: | |
c08136a3 JG |
132 | lttng_condition_put(rotate_condition); |
133 | lttng_action_put(notify_action); | |
134 | if (ret) { | |
135 | lttng_trigger_put(session->rotate_trigger); | |
136 | } | |
90936dcf JD |
137 | return ret; |
138 | } | |
139 | ||
28ab034a JG |
140 | int unsubscribe_session_consumed_size_rotation( |
141 | struct ltt_session *session, struct notification_thread_handle *notification_thread_handle) | |
90936dcf JD |
142 | { |
143 | int ret = 0; | |
144 | enum lttng_notification_channel_status status; | |
dc65dda3 | 145 | const uint64_t eventfd_increment_value = 1; |
90936dcf | 146 | |
c08136a3 | 147 | LTTNG_ASSERT(session->rotate_trigger); |
90936dcf | 148 | status = lttng_notification_channel_unsubscribe( |
28ab034a JG |
149 | rotate_notification_channel, |
150 | lttng_trigger_get_const_condition(session->rotate_trigger)); | |
90936dcf JD |
151 | if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { |
152 | ERR("Session unsubscribe error: %d", (int) status); | |
153 | ret = -1; | |
154 | goto end; | |
155 | } | |
156 | ||
dc65dda3 JG |
157 | ret = lttng_write(rotate_notification_channel_subscription_change_eventfd, |
158 | &eventfd_increment_value, | |
159 | sizeof(eventfd_increment_value)); | |
160 | if (ret != sizeof(eventfd_increment_value)) { | |
161 | PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed"); | |
162 | ret = -1; | |
163 | goto end; | |
164 | } | |
165 | ||
28ab034a JG |
166 | ret = notification_thread_command_unregister_trigger(notification_thread_handle, |
167 | session->rotate_trigger); | |
90936dcf JD |
168 | if (ret != LTTNG_OK) { |
169 | ERR("Session unregister trigger error: %d", ret); | |
170 | goto end; | |
171 | } | |
172 | ||
c08136a3 JG |
173 | lttng_trigger_put(session->rotate_trigger); |
174 | session->rotate_trigger = nullptr; | |
175 | ||
90936dcf JD |
176 | ret = 0; |
177 | end: | |
178 | return ret; | |
179 | } |