2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/utils.h>
24 struct cmd_clear_session_reply_context
{
29 void cmd_clear_session_reply(const struct ltt_session
*session
,
34 const struct cmd_clear_session_reply_context
*reply_context
=
36 struct lttcomm_lttng_msg llm
= {
37 .cmd_type
= LTTNG_CLEAR_SESSION
,
44 DBG("End of clear command: replying to client");
45 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
47 if (comm_ret
!= (ssize_t
) sizeof(llm
)) {
48 ERR("Failed to send result of session \"%s\" clear to client",
51 ret
= close(reply_context
->reply_sock_fd
);
53 PERROR("Failed to close client socket in deferred session clear reply");
58 int cmd_clear_session(struct ltt_session
*session
, int *sock_fd
)
61 struct cmd_clear_session_reply_context
*reply_context
= NULL
;
62 bool session_was_active
= false;
63 struct ltt_kernel_session
*ksession
;
64 struct ltt_ust_session
*usess
;
66 ksession
= session
->kernel_session
;
67 usess
= session
->ust_session
;
70 reply_context
= zmalloc(sizeof(*reply_context
));
72 ret
= LTTNG_ERR_NOMEM
;
75 reply_context
->reply_sock_fd
= *sock_fd
;
78 if (!session
->has_been_started
) {
80 * Nothing to be cleared, this is not an error: there is
81 * indeed nothing to do, and there is no reason why we
82 * should return an error to the user.
87 /* Unsupported feature in lttng-relayd before 2.11. */
88 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
89 (session
->consumer
->relay_major_version
== 2 &&
90 session
->consumer
->relay_minor_version
< 12)) {
91 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
94 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
95 !session
->consumer
->relay_allows_clear
) {
96 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
101 * After a stop followed by a clear, all subsequent clear are
102 * effect-less until start is performed.
104 if (session
->cleared_after_last_stop
) {
110 * After a stop followed by a rotation, all subsequent clear are effect-less
111 * until start is performed.
113 if (session
->rotated_after_last_stop
) {
118 session_was_active
= session
->active
;
119 if (session_was_active
) {
120 ret
= stop_kernel_session(ksession
);
121 if (ret
!= LTTNG_OK
) {
124 if (usess
&& usess
->active
) {
125 ret
= ust_app_stop_trace_all(usess
);
127 ret
= LTTNG_ERR_UST_STOP_FAIL
;
134 * Clear active kernel and UST session buffers.
136 if (session
->kernel_session
) {
137 ret
= kernel_clear_session(session
);
138 if (ret
!= LTTNG_OK
) {
142 if (session
->ust_session
) {
143 ret
= ust_app_clear_session(session
);
144 if (ret
!= LTTNG_OK
) {
149 if (session
->output_traces
) {
151 * Use rotation to delete local and remote stream files.
154 ret
= session_add_clear_notifier(session
,
155 cmd_clear_session_reply
,
156 (void *) reply_context
);
158 ret
= LTTNG_ERR_FATAL
;
162 * On success, ownership of reply_context has been
163 * passed to session_add_clear_notifier().
165 reply_context
= NULL
;
168 ret
= cmd_rotate_session(session
, NULL
, true,
169 LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
);
170 if (ret
!= LTTNG_OK
) {
174 if (!session
->active
) {
175 session
->cleared_after_last_stop
= true;
177 if (session_was_active
) {
179 if (ksession
!= NULL
) {
180 DBG("Start kernel tracing session \"%s\"",
182 ret
= start_kernel_session(ksession
);
183 if (ret
!= LTTNG_OK
) {
188 /* Flag session that trace should start automatically */
190 int int_ret
= ust_app_start_trace_all(usess
);
193 ret
= LTTNG_ERR_UST_START_FAIL
;
199 * Open a packet in every stream of the session to ensure that
200 * viewers can correctly identify the boundaries of the periods
201 * during which tracing was active for this session.
203 ret
= session_open_packets(session
);
204 if (ret
!= LTTNG_OK
) {