Fix: sessiond: size-based notification occasionally not triggered
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
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 <lttng/trigger/trigger.h>
11 #include <common/error.h>
12 #include <common/config/session-config.h>
13 #include <common/defaults.h>
14 #include <common/utils.h>
15 #include <common/futex.h>
16 #include <common/align.h>
17 #include <common/time.h>
18 #include <common/hashtable/utils.h>
19 #include <common/kernel-ctl/kernel-ctl.h>
20 #include <common/credentials.h>
21 #include <sys/stat.h>
22 #include <time.h>
23 #include <signal.h>
24 #include <inttypes.h>
25
26 #include <lttng/notification/channel-internal.h>
27 #include <lttng/rotate-internal.h>
28 #include <lttng/condition/condition-internal.h>
29 #include <lttng/action/action-internal.h>
30
31 #include "session.h"
32 #include "rotate.h"
33 #include "rotation-thread.h"
34 #include "lttng-sessiond.h"
35 #include "health-sessiond.h"
36 #include "cmd.h"
37 #include "utils.h"
38 #include "notification-thread-commands.h"
39
40 #include <urcu.h>
41 #include <urcu/list.h>
42 #include <urcu/rculfhash.h>
43
44 int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size,
45 struct notification_thread_handle *notification_thread_handle)
46 {
47 int ret;
48 enum lttng_condition_status condition_status;
49 enum lttng_notification_channel_status nc_status;
50 const uint64_t eventfd_increment_value = 1;
51 const struct lttng_credentials session_creds = {
52 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
53 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
54 };
55 struct lttng_condition *rotate_condition;
56 struct lttng_action *notify_action = NULL;
57
58 rotate_condition = lttng_condition_session_consumed_size_create();
59 if (!rotate_condition) {
60 ERR("Failed to create session consumed size condition object");
61 ret = -1;
62 goto end;
63 }
64
65 condition_status = lttng_condition_session_consumed_size_set_threshold(
66 rotate_condition, size);
67 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
68 ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
69 size);
70 ret = -1;
71 goto end;
72 }
73
74 condition_status =
75 lttng_condition_session_consumed_size_set_session_name(
76 rotate_condition, 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 assert(!session->rotate_trigger);
92 session->rotate_trigger = lttng_trigger_create(rotate_condition,
93 notify_action);
94 if (!session->rotate_trigger) {
95 ERR("Could not create size-based rotation trigger");
96 ret = -1;
97 goto end;
98 }
99
100 /* Ensure this trigger is not visible to external users. */
101 lttng_trigger_set_hidden(session->rotate_trigger);
102 lttng_trigger_set_credentials(
103 session->rotate_trigger, &session_creds);
104
105 nc_status = lttng_notification_channel_subscribe(
106 rotate_notification_channel, rotate_condition);
107 if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
108 ERR("Could not subscribe to session consumed size notification");
109 ret = -1;
110 goto end;
111 }
112
113 ret = lttng_write(rotate_notification_channel_subscription_change_eventfd,
114 &eventfd_increment_value,
115 sizeof(eventfd_increment_value));
116 if (ret != sizeof(eventfd_increment_value)) {
117 PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed");
118 ret = -1;
119 goto end;
120 }
121
122 ret = notification_thread_command_register_trigger(
123 notification_thread_handle, session->rotate_trigger,
124 true);
125 if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
126 ERR("Register trigger, %s", lttng_strerror(ret));
127 ret = -1;
128 goto end;
129 }
130
131 ret = 0;
132
133 end:
134 lttng_condition_put(rotate_condition);
135 lttng_action_put(notify_action);
136 if (ret) {
137 lttng_trigger_put(session->rotate_trigger);
138 }
139 return ret;
140 }
141
142 int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
143 struct notification_thread_handle *notification_thread_handle)
144 {
145 int ret = 0;
146 enum lttng_notification_channel_status status;
147 const uint64_t eventfd_increment_value = 1;
148
149 assert(session->rotate_trigger);
150 status = lttng_notification_channel_unsubscribe(
151 rotate_notification_channel,
152 lttng_trigger_get_const_condition(session->rotate_trigger));
153 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
154 ERR("Session unsubscribe error: %d", (int) status);
155 ret = -1;
156 goto end;
157 }
158
159 ret = lttng_write(rotate_notification_channel_subscription_change_eventfd,
160 &eventfd_increment_value,
161 sizeof(eventfd_increment_value));
162 if (ret != sizeof(eventfd_increment_value)) {
163 PERROR("Failed to wake up rotation thread as writing to the rotation thread notification channel subscription change eventfd failed");
164 ret = -1;
165 goto end;
166 }
167
168 ret = notification_thread_command_unregister_trigger(notification_thread_handle,
169 session->rotate_trigger);
170 if (ret != LTTNG_OK) {
171 ERR("Session unregister trigger error: %d", ret);
172 goto end;
173 }
174
175 lttng_trigger_put(session->rotate_trigger);
176 session->rotate_trigger = NULL;
177
178 ret = 0;
179 end:
180 return ret;
181 }
This page took 0.033969 seconds and 4 git commands to generate.