Commit | Line | Data |
---|---|---|
022349df | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
022349df | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
022349df | 5 | * |
022349df MD |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
022349df MD |
9 | #include <inttypes.h> |
10 | #include <string.h> | |
11 | #include <unistd.h> | |
12 | ||
13 | #include <common/defaults.h> | |
14 | #include <common/error.h> | |
15 | #include <common/utils.h> | |
16 | ||
17 | #include "clear.h" | |
18 | #include "session.h" | |
19 | #include "ust-app.h" | |
20 | #include "kernel.h" | |
21 | #include "cmd.h" | |
22 | ||
23 | struct cmd_clear_session_reply_context { | |
24 | int reply_sock_fd; | |
25 | }; | |
26 | ||
27 | static | |
28 | void cmd_clear_session_reply(const struct ltt_session *session, | |
29 | void *_reply_context) | |
30 | { | |
31 | int ret; | |
32 | ssize_t comm_ret; | |
33 | const struct cmd_clear_session_reply_context *reply_context = | |
7966af57 | 34 | (cmd_clear_session_reply_context *) _reply_context; |
022349df MD |
35 | struct lttcomm_lttng_msg llm = { |
36 | .cmd_type = LTTNG_CLEAR_SESSION, | |
37 | .ret_code = LTTNG_OK, | |
38 | .pid = UINT32_MAX, | |
39 | .cmd_header_size = 0, | |
40 | .data_size = 0, | |
41 | }; | |
42 | ||
43 | DBG("End of clear command: replying to client"); | |
44 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, | |
45 | &llm, sizeof(llm)); | |
46 | if (comm_ret != (ssize_t) sizeof(llm)) { | |
47 | ERR("Failed to send result of session \"%s\" clear to client", | |
48 | session->name); | |
49 | } | |
50 | ret = close(reply_context->reply_sock_fd); | |
51 | if (ret) { | |
52 | PERROR("Failed to close client socket in deferred session clear reply"); | |
53 | } | |
54 | free(_reply_context); | |
55 | } | |
56 | ||
57 | int cmd_clear_session(struct ltt_session *session, int *sock_fd) | |
58 | { | |
59 | int ret = LTTNG_OK; | |
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; | |
64 | ||
65 | ksession = session->kernel_session; | |
66 | usess = session->ust_session; | |
67 | ||
68 | if (sock_fd) { | |
7966af57 | 69 | reply_context = (cmd_clear_session_reply_context *) zmalloc(sizeof(*reply_context)); |
022349df MD |
70 | if (!reply_context) { |
71 | ret = LTTNG_ERR_NOMEM; | |
72 | goto end; | |
73 | } | |
74 | reply_context->reply_sock_fd = *sock_fd; | |
75 | } | |
76 | ||
77 | if (!session->has_been_started) { | |
78 | /* | |
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. | |
82 | */ | |
83 | goto end; | |
84 | } | |
85 | ||
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; | |
91 | goto end; | |
92 | } | |
93 | if (session->consumer->type == CONSUMER_DST_NET && | |
94 | !session->consumer->relay_allows_clear) { | |
95 | ret = LTTNG_ERR_CLEAR_NOT_AVAILABLE_RELAY; | |
96 | goto end; | |
97 | } | |
98 | ||
99 | /* | |
100 | * After a stop followed by a clear, all subsequent clear are | |
101 | * effect-less until start is performed. | |
102 | */ | |
103 | if (session->cleared_after_last_stop) { | |
104 | ret = LTTNG_OK; | |
105 | goto end; | |
106 | } | |
107 | ||
108 | /* | |
109 | * After a stop followed by a rotation, all subsequent clear are effect-less | |
110 | * until start is performed. | |
111 | */ | |
112 | if (session->rotated_after_last_stop) { | |
113 | ret = LTTNG_OK; | |
114 | goto end; | |
115 | } | |
116 | ||
117 | session_was_active = session->active; | |
118 | if (session_was_active) { | |
119 | ret = stop_kernel_session(ksession); | |
120 | if (ret != LTTNG_OK) { | |
121 | goto end; | |
122 | } | |
123 | if (usess && usess->active) { | |
124 | ret = ust_app_stop_trace_all(usess); | |
125 | if (ret < 0) { | |
126 | ret = LTTNG_ERR_UST_STOP_FAIL; | |
127 | goto end; | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | /* | |
133 | * Clear active kernel and UST session buffers. | |
134 | */ | |
135 | if (session->kernel_session) { | |
136 | ret = kernel_clear_session(session); | |
137 | if (ret != LTTNG_OK) { | |
138 | goto end; | |
139 | } | |
140 | } | |
141 | if (session->ust_session) { | |
142 | ret = ust_app_clear_session(session); | |
143 | if (ret != LTTNG_OK) { | |
144 | goto end; | |
145 | } | |
146 | } | |
147 | ||
148 | if (session->output_traces) { | |
149 | /* | |
150 | * Use rotation to delete local and remote stream files. | |
151 | */ | |
152 | if (reply_context) { | |
153 | ret = session_add_clear_notifier(session, | |
154 | cmd_clear_session_reply, | |
155 | (void *) reply_context); | |
156 | if (ret) { | |
157 | ret = LTTNG_ERR_FATAL; | |
158 | goto end; | |
159 | } | |
160 | /* | |
161 | * On success, ownership of reply_context has been | |
162 | * passed to session_add_clear_notifier(). | |
163 | */ | |
164 | reply_context = NULL; | |
165 | *sock_fd = -1; | |
166 | } | |
167 | ret = cmd_rotate_session(session, NULL, true, | |
168 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE); | |
169 | if (ret != LTTNG_OK) { | |
170 | goto end; | |
171 | } | |
172 | } | |
173 | if (!session->active) { | |
174 | session->cleared_after_last_stop = true; | |
175 | } | |
176 | if (session_was_active) { | |
177 | /* Kernel tracing */ | |
178 | if (ksession != NULL) { | |
179 | DBG("Start kernel tracing session \"%s\"", | |
180 | session->name); | |
181 | ret = start_kernel_session(ksession); | |
182 | if (ret != LTTNG_OK) { | |
183 | goto end; | |
184 | } | |
185 | } | |
186 | ||
187 | /* Flag session that trace should start automatically */ | |
188 | if (usess) { | |
189 | int int_ret = ust_app_start_trace_all(usess); | |
190 | ||
191 | if (int_ret < 0) { | |
192 | ret = LTTNG_ERR_UST_START_FAIL; | |
193 | goto end; | |
194 | } | |
195 | } | |
04ed9e10 JG |
196 | |
197 | /* | |
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. | |
201 | */ | |
202 | ret = session_open_packets(session); | |
203 | if (ret != LTTNG_OK) { | |
204 | goto end; | |
205 | } | |
022349df MD |
206 | } |
207 | ret = LTTNG_OK; | |
208 | end: | |
209 | free(reply_context); | |
210 | return ret; | |
211 | } |