2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <common/defaults.h>
14 #include <common/error.h>
15 #include <common/utils.h>
23 struct cmd_clear_session_reply_context
{
28 void cmd_clear_session_reply(const struct ltt_session
*session
,
33 const struct cmd_clear_session_reply_context
*reply_context
=
34 (cmd_clear_session_reply_context
*) _reply_context
;
35 struct lttcomm_lttng_msg llm
= {
36 .cmd_type
= LTTNG_CLEAR_SESSION
,
43 DBG("End of clear command: replying to client");
44 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
46 if (comm_ret
!= (ssize_t
) sizeof(llm
)) {
47 ERR("Failed to send result of session \"%s\" clear to client",
50 ret
= close(reply_context
->reply_sock_fd
);
52 PERROR("Failed to close client socket in deferred session clear reply");
57 int cmd_clear_session(struct ltt_session
*session
, int *sock_fd
)
60 struct cmd_clear_session_reply_context
*reply_context
= NULL
;
61 bool session_was_active
= false;
62 struct ltt_kernel_session
*ksession
;
63 struct ltt_ust_session
*usess
;
65 ksession
= session
->kernel_session
;
66 usess
= session
->ust_session
;
69 reply_context
= (cmd_clear_session_reply_context
*) zmalloc(sizeof(*reply_context
));
71 ret
= LTTNG_ERR_NOMEM
;
74 reply_context
->reply_sock_fd
= *sock_fd
;
77 if (!session
->has_been_started
) {
79 * Nothing to be cleared, this is not an error: there is
80 * indeed nothing to do, and there is no reason why we
81 * should return an error to the user.
86 /* Unsupported feature in lttng-relayd before 2.11. */
87 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
88 (session
->consumer
->relay_major_version
== 2 &&
89 session
->consumer
->relay_minor_version
< 12)) {
90 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
93 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
94 !session
->consumer
->relay_allows_clear
) {
95 ret
= LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY
;
100 * After a stop followed by a clear, all subsequent clear are
101 * effect-less until start is performed.
103 if (session
->cleared_after_last_stop
) {
109 * After a stop followed by a rotation, all subsequent clear are effect-less
110 * until start is performed.
112 if (session
->rotated_after_last_stop
) {
117 session_was_active
= session
->active
;
118 if (session_was_active
) {
119 ret
= stop_kernel_session(ksession
);
120 if (ret
!= LTTNG_OK
) {
123 if (usess
&& usess
->active
) {
124 ret
= ust_app_stop_trace_all(usess
);
126 ret
= LTTNG_ERR_UST_STOP_FAIL
;
133 * Clear active kernel and UST session buffers.
135 if (session
->kernel_session
) {
136 ret
= kernel_clear_session(session
);
137 if (ret
!= LTTNG_OK
) {
141 if (session
->ust_session
) {
142 ret
= ust_app_clear_session(session
);
143 if (ret
!= LTTNG_OK
) {
148 if (session
->output_traces
) {
150 * Use rotation to delete local and remote stream files.
153 ret
= session_add_clear_notifier(session
,
154 cmd_clear_session_reply
,
155 (void *) reply_context
);
157 ret
= LTTNG_ERR_FATAL
;
161 * On success, ownership of reply_context has been
162 * passed to session_add_clear_notifier().
164 reply_context
= NULL
;
167 ret
= cmd_rotate_session(session
, NULL
, true,
168 LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
);
169 if (ret
!= LTTNG_OK
) {
173 if (!session
->active
) {
174 session
->cleared_after_last_stop
= true;
176 if (session_was_active
) {
178 if (ksession
!= NULL
) {
179 DBG("Start kernel tracing session \"%s\"",
181 ret
= start_kernel_session(ksession
);
182 if (ret
!= LTTNG_OK
) {
187 /* Flag session that trace should start automatically */
189 int int_ret
= ust_app_start_trace_all(usess
);
192 ret
= LTTNG_ERR_UST_START_FAIL
;
198 * Open a packet in every stream of the session to ensure that
199 * viewers can correctly identify the boundaries of the periods
200 * during which tracing was active for this session.
202 ret
= session_open_packets(session
);
203 if (ret
!= LTTNG_OK
) {