sessiond: Replace uses of session_trylock_list by a dedicated assert macro
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.cpp
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>
c9e313bc
SM
11#include <common/error.hpp>
12#include <common/config/session-config.hpp>
13#include <common/defaults.hpp>
14#include <common/utils.hpp>
15#include <common/futex.hpp>
16#include <common/align.hpp>
17#include <common/time.hpp>
18#include <common/hashtable/utils.hpp>
19#include <common/kernel-ctl/kernel-ctl.hpp>
20#include <common/credentials.hpp>
db66e574
JD
21#include <sys/stat.h>
22#include <time.h>
23#include <signal.h>
24#include <inttypes.h>
25
c9e313bc
SM
26#include <lttng/notification/channel-internal.hpp>
27#include <lttng/rotate-internal.hpp>
c08136a3
JG
28#include <lttng/condition/condition-internal.hpp>
29#include <lttng/action/action-internal.hpp>
d88744a4 30
c9e313bc
SM
31#include "session.hpp"
32#include "rotate.hpp"
33#include "rotation-thread.hpp"
34#include "lttng-sessiond.hpp"
35#include "health-sessiond.hpp"
36#include "cmd.hpp"
37#include "utils.hpp"
38#include "notification-thread-commands.hpp"
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;
c08136a3
JG
50 struct lttng_condition *rotate_condition = nullptr;
51 struct lttng_action *notify_action = nullptr;
3da864a9 52 const struct lttng_credentials session_creds = {
ff588497
JR
53 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
54 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
3da864a9 55 };
90936dcf 56
c08136a3
JG
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(
c08136a3 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(
c08136a3 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
c08136a3
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
c08136a3
JG
90 LTTNG_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
f2bda80e
JG
99 /* Ensure this trigger is not visible to external users. */
100 lttng_trigger_set_hidden(session->rotate_trigger);
3da864a9
JR
101 lttng_trigger_set_credentials(
102 session->rotate_trigger, &session_creds);
103
90936dcf 104 nc_status = lttng_notification_channel_subscribe(
c08136a3 105 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
112 ret = notification_thread_command_register_trigger(
0efb2ad7
JG
113 notification_thread_handle, session->rotate_trigger,
114 true);
90936dcf
JD
115 if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
116 ERR("Register trigger, %s", lttng_strerror(ret));
117 ret = -1;
118 goto end;
119 }
120
121 ret = 0;
122
123end:
c08136a3
JG
124 lttng_condition_put(rotate_condition);
125 lttng_action_put(notify_action);
126 if (ret) {
127 lttng_trigger_put(session->rotate_trigger);
128 }
90936dcf
JD
129 return ret;
130}
131
132int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
133 struct notification_thread_handle *notification_thread_handle)
134{
135 int ret = 0;
136 enum lttng_notification_channel_status status;
137
c08136a3 138 LTTNG_ASSERT(session->rotate_trigger);
90936dcf
JD
139 status = lttng_notification_channel_unsubscribe(
140 rotate_notification_channel,
c08136a3 141 lttng_trigger_get_const_condition(session->rotate_trigger));
90936dcf
JD
142 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
143 ERR("Session unsubscribe error: %d", (int) status);
144 ret = -1;
145 goto end;
146 }
147
148 ret = notification_thread_command_unregister_trigger(
149 notification_thread_handle, session->rotate_trigger);
150 if (ret != LTTNG_OK) {
151 ERR("Session unregister trigger error: %d", ret);
152 goto end;
153 }
154
c08136a3
JG
155 lttng_trigger_put(session->rotate_trigger);
156 session->rotate_trigger = nullptr;
157
90936dcf
JD
158 ret = 0;
159end:
160 return ret;
161}
This page took 0.055096 seconds and 4 git commands to generate.