2 * Copyright (C) 2019 - Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/defaults.h>
25 #include <common/error.h>
26 #include <common/utils.h>
34 struct cmd_clear_session_reply_context
{
39 void cmd_clear_session_reply(const struct ltt_session
*session
,
44 const struct cmd_clear_session_reply_context
*reply_context
=
46 struct lttcomm_lttng_msg llm
= {
47 .cmd_type
= LTTNG_CLEAR_SESSION
,
54 DBG("End of clear command: replying to client");
55 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
57 if (comm_ret
!= (ssize_t
) sizeof(llm
)) {
58 ERR("Failed to send result of session \"%s\" clear to client",
61 ret
= close(reply_context
->reply_sock_fd
);
63 PERROR("Failed to close client socket in deferred session clear reply");
68 int cmd_clear_session(struct ltt_session
*session
, int *sock_fd
)
71 struct cmd_clear_session_reply_context
*reply_context
= NULL
;
72 bool session_was_active
= false;
73 struct ltt_kernel_session
*ksession
;
74 struct ltt_ust_session
*usess
;
76 ksession
= session
->kernel_session
;
77 usess
= session
->ust_session
;
80 reply_context
= zmalloc(sizeof(*reply_context
));
82 ret
= LTTNG_ERR_NOMEM
;
85 reply_context
->reply_sock_fd
= *sock_fd
;
88 if (!session
->has_been_started
) {
90 * Nothing to be cleared, this is not an error: there is
91 * indeed nothing to do, and there is no reason why we
92 * should return an error to the user.
97 /* Unsupported feature in lttng-relayd before 2.11. */
98 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
99 (session
->consumer
->relay_major_version
== 2 &&
100 session
->consumer
->relay_minor_version
< 12)) {
101 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
104 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
105 !session
->consumer
->relay_allows_clear
) {
106 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
111 * After a stop followed by a clear, all subsequent clear are
112 * effect-less until start is performed.
114 if (session
->cleared_after_last_stop
) {
120 * After a stop followed by a rotation, all subsequent clear are effect-less
121 * until start is performed.
123 if (session
->rotated_after_last_stop
) {
128 session_was_active
= session
->active
;
129 if (session_was_active
) {
130 ret
= stop_kernel_session(ksession
);
131 if (ret
!= LTTNG_OK
) {
134 if (usess
&& usess
->active
) {
135 ret
= ust_app_stop_trace_all(usess
);
137 ret
= LTTNG_ERR_UST_STOP_FAIL
;
144 * Clear active kernel and UST session buffers.
146 if (session
->kernel_session
) {
147 ret
= kernel_clear_session(session
);
148 if (ret
!= LTTNG_OK
) {
152 if (session
->ust_session
) {
153 ret
= ust_app_clear_session(session
);
154 if (ret
!= LTTNG_OK
) {
159 if (session
->output_traces
) {
161 * Use rotation to delete local and remote stream files.
164 ret
= session_add_clear_notifier(session
,
165 cmd_clear_session_reply
,
166 (void *) reply_context
);
168 ret
= LTTNG_ERR_FATAL
;
172 * On success, ownership of reply_context has been
173 * passed to session_add_clear_notifier().
175 reply_context
= NULL
;
178 ret
= cmd_rotate_session(session
, NULL
, true,
179 LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
);
180 if (ret
!= LTTNG_OK
) {
184 if (!session
->active
) {
185 session
->cleared_after_last_stop
= true;
187 if (session_was_active
) {
189 if (ksession
!= NULL
) {
190 DBG("Start kernel tracing session \"%s\"",
192 ret
= start_kernel_session(ksession
);
193 if (ret
!= LTTNG_OK
) {
198 /* Flag session that trace should start automatically */
200 int int_ret
= ust_app_start_trace_all(usess
);
203 ret
= LTTNG_ERR_UST_START_FAIL
;