Fix: sessiond: rotation trigger leak
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
CommitLineData
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
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 <sys/eventfd.h>
21#include <sys/stat.h>
22#include <time.h>
23#include <signal.h>
24#include <inttypes.h>
25
90936dcf 26#include <lttng/notification/channel-internal.h>
d88744a4 27#include <lttng/rotate-internal.h>
69d5d2ae
JG
28#include <lttng/condition/condition-internal.h>
29#include <lttng/action/action-internal.h>
d88744a4 30
db66e574
JD
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"
90936dcf 38#include "notification-thread-commands.h"
db66e574
JD
39
40#include <urcu.h>
41#include <urcu/list.h>
42#include <urcu/rculfhash.h>
43
90936dcf
JD
44int 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;
69d5d2ae
JG
50 struct lttng_condition *rotate_condition = NULL;
51 struct lttng_action *notify_action = NULL;
52 const struct lttng_credentials session_creds = {
53 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
54 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
55 };
56
57 rotate_condition = lttng_condition_session_consumed_size_create();
58 if (!rotate_condition) {
90936dcf
JD
59 ERR("Failed to create session consumed size condition object");
60 ret = -1;
61 goto end;
62 }
63
64 condition_status = lttng_condition_session_consumed_size_set_threshold(
69d5d2ae 65 rotate_condition, size);
90936dcf
JD
66 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
67 ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
68 size);
69 ret = -1;
70 goto end;
71 }
72
73 condition_status =
74 lttng_condition_session_consumed_size_set_session_name(
69d5d2ae 75 rotate_condition, session->name);
90936dcf
JD
76 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
77 ERR("Could not set session consumed size condition session name (name = %s)",
78 session->name);
79 ret = -1;
80 goto end;
81 }
82
69d5d2ae
JG
83 notify_action = lttng_action_notify_create();
84 if (!notify_action) {
90936dcf
JD
85 ERR("Could not create notify action");
86 ret = -1;
87 goto end;
88 }
89
69d5d2ae
JG
90 assert(!session->rotate_trigger);
91 session->rotate_trigger = lttng_trigger_create(rotate_condition,
92 notify_action);
90936dcf
JD
93 if (!session->rotate_trigger) {
94 ERR("Could not create size-based rotation trigger");
95 ret = -1;
96 goto end;
97 }
98
99 nc_status = lttng_notification_channel_subscribe(
69d5d2ae 100 rotate_notification_channel, rotate_condition);
90936dcf
JD
101 if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
102 ERR("Could not subscribe to session consumed size notification");
103 ret = -1;
104 goto end;
105 }
106
107 ret = notification_thread_command_register_trigger(
108 notification_thread_handle, session->rotate_trigger);
109 if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
110 ERR("Register trigger, %s", lttng_strerror(ret));
111 ret = -1;
112 goto end;
113 }
114
115 ret = 0;
116
117end:
69d5d2ae
JG
118 lttng_condition_put(rotate_condition);
119 lttng_action_put(notify_action);
120 if (ret) {
121 lttng_trigger_put(session->rotate_trigger);
122 }
90936dcf
JD
123 return ret;
124}
125
126int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
127 struct notification_thread_handle *notification_thread_handle)
128{
129 int ret = 0;
130 enum lttng_notification_channel_status status;
131
69d5d2ae 132 assert(session->rotate_trigger);
90936dcf
JD
133 status = lttng_notification_channel_unsubscribe(
134 rotate_notification_channel,
69d5d2ae 135 lttng_trigger_get_const_condition(session->rotate_trigger));
90936dcf
JD
136 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
137 ERR("Session unsubscribe error: %d", (int) status);
138 ret = -1;
139 goto end;
140 }
141
142 ret = notification_thread_command_unregister_trigger(
143 notification_thread_handle, session->rotate_trigger);
144 if (ret != LTTNG_OK) {
145 ERR("Session unregister trigger error: %d", ret);
146 goto end;
147 }
148
69d5d2ae
JG
149 lttng_trigger_put(session->rotate_trigger);
150 session->rotate_trigger = NULL;
151
90936dcf
JD
152 ret = 0;
153end:
154 return ret;
155}
This page took 0.05622 seconds and 4 git commands to generate.