Fix: relayd outputs traces of legacy sessionds to home dir
[lttng-tools.git] / src / bin / lttng-relayd / session.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _LGPL_SOURCE
21 #include <common/common.h>
22 #include <common/utils.h>
23 #include <common/compat/uuid.h>
24 #include <urcu/rculist.h>
25
26 #include "ctf-trace.h"
27 #include "lttng-relayd.h"
28 #include "session.h"
29 #include "sessiond-trace-chunks.h"
30 #include "stream.h"
31 #include <common/defaults.h>
32
33 /* Global session id used in the session creation. */
34 static uint64_t last_relay_session_id;
35 static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
36
37 static int session_set_anonymous_chunk(struct relay_session *session)
38 {
39 int ret = 0;
40 struct lttng_trace_chunk *chunk = NULL;
41 enum lttng_trace_chunk_status status;
42 struct lttng_directory_handle output_directory;
43 char *output_path;
44 bool output_path_allocated = false;
45
46 if (!opt_output_path) {
47 /* No output path defined */
48 const char *home_dir = utils_get_home_dir();
49 if (!home_dir) {
50 ERR("Home path not found.\n \
51 Please specify an output path using -o, --output PATH");
52 ret = -1;
53 goto end;
54 }
55 ret = asprintf(&output_path, "%s/%s", home_dir, DEFAULT_TRACE_DIR_NAME);
56 if (ret < 0) {
57 PERROR("asprintf trace dir name");
58 ret = -1;
59 goto end;
60 }
61 output_path_allocated = true;
62 } else {
63 output_path = opt_output_path;
64 output_path_allocated = false;
65 }
66
67 ret = lttng_directory_handle_init(&output_directory, output_path);
68 if (ret) {
69 goto end;
70 }
71
72 chunk = lttng_trace_chunk_create_anonymous();
73 if (!chunk) {
74 goto end;
75 }
76
77 status = lttng_trace_chunk_set_credentials_current_user(chunk);
78 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
79 ret = -1;
80 goto end;
81 }
82
83 status = lttng_trace_chunk_set_as_owner(chunk, &output_directory);
84 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
85 ret = -1;
86 goto end;
87 }
88 session->current_trace_chunk = chunk;
89 chunk = NULL;
90 end:
91 lttng_trace_chunk_put(chunk);
92 lttng_directory_handle_fini(&output_directory);
93 if (output_path_allocated) {
94 free(output_path);
95 }
96 return ret;
97 }
98
99 /*
100 * Create a new session by assigning a new session ID.
101 *
102 * Return allocated session or else NULL.
103 */
104 struct relay_session *session_create(const char *session_name,
105 const char *hostname, const char *base_path,
106 uint32_t live_timer,
107 bool snapshot,
108 const lttng_uuid sessiond_uuid,
109 const uint64_t *id_sessiond,
110 const uint64_t *current_chunk_id,
111 const time_t *creation_time,
112 uint32_t major,
113 uint32_t minor,
114 bool session_name_contains_creation_time)
115 {
116 int ret;
117 struct relay_session *session = NULL;
118
119 if (session_name && strstr(session_name, ".")) {
120 ERR("Illegal character in session name: \"%s\"",
121 session_name);
122 goto error;
123 }
124 if (base_path && strstr(base_path, "../")) {
125 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
126 base_path);
127 goto error;
128 }
129 if (hostname && strstr(hostname, ".")) {
130 ERR("Invalid character in hostname: \"%s\"",
131 hostname);
132 goto error;
133 }
134
135 session = zmalloc(sizeof(*session));
136 if (!session) {
137 PERROR("Failed to allocate session");
138 goto error;
139 }
140 if (lttng_strncpy(session->session_name, session_name,
141 sizeof(session->session_name))) {
142 WARN("Session name exceeds maximal allowed length");
143 goto error;
144 }
145 if (lttng_strncpy(session->hostname, hostname,
146 sizeof(session->hostname))) {
147 WARN("Hostname exceeds maximal allowed length");
148 goto error;
149 }
150 if (lttng_strncpy(session->base_path, base_path,
151 sizeof(session->base_path))) {
152 WARN("Base path exceeds maximal allowed length");
153 goto error;
154 }
155 if (creation_time) {
156 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
157 }
158 session->session_name_contains_creation_time =
159 session_name_contains_creation_time;
160
161 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
162 if (!session->ctf_traces_ht) {
163 goto error;
164 }
165
166 pthread_mutex_lock(&last_relay_session_id_lock);
167 session->id = ++last_relay_session_id;
168 pthread_mutex_unlock(&last_relay_session_id_lock);
169
170 session->major = major;
171 session->minor = minor;
172 lttng_ht_node_init_u64(&session->session_n, session->id);
173 urcu_ref_init(&session->ref);
174 CDS_INIT_LIST_HEAD(&session->recv_list);
175 pthread_mutex_init(&session->lock, NULL);
176 pthread_mutex_init(&session->recv_list_lock, NULL);
177
178 session->live_timer = live_timer;
179 session->snapshot = snapshot;
180 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
181
182 if (id_sessiond) {
183 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
184 }
185
186 ret = sessiond_trace_chunk_registry_session_created(
187 sessiond_trace_chunk_registry, sessiond_uuid);
188 if (ret) {
189 goto error;
190 }
191
192 if (id_sessiond && current_chunk_id) {
193 session->current_trace_chunk =
194 sessiond_trace_chunk_registry_get_chunk(
195 sessiond_trace_chunk_registry,
196 session->sessiond_uuid,
197 session->id_sessiond.value,
198 *current_chunk_id);
199 if (!session->current_trace_chunk) {
200 char uuid_str[UUID_STR_LEN];
201
202 lttng_uuid_to_str(sessiond_uuid, uuid_str);
203 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
204 uuid_str, *id_sessiond,
205 *current_chunk_id);
206 }
207 } else if (!id_sessiond) {
208 /*
209 * Pre-2.11 peers will not announce trace chunks. An
210 * anonymous trace chunk which will remain set for the
211 * duration of the session is created.
212 */
213 ret = session_set_anonymous_chunk(session);
214 if (ret) {
215 goto error;
216 }
217 }
218
219 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
220 return session;
221
222 error:
223 session_put(session);
224 return NULL;
225 }
226
227 /* Should be called with RCU read-side lock held. */
228 bool session_get(struct relay_session *session)
229 {
230 return urcu_ref_get_unless_zero(&session->ref);
231 }
232
233 /*
234 * Lookup a session within the session hash table using the session id
235 * as key. A session reference is taken when a session is returned.
236 * session_put() must be called on that session.
237 *
238 * Return session or NULL if not found.
239 */
240 struct relay_session *session_get_by_id(uint64_t id)
241 {
242 struct relay_session *session = NULL;
243 struct lttng_ht_node_u64 *node;
244 struct lttng_ht_iter iter;
245
246 rcu_read_lock();
247 lttng_ht_lookup(sessions_ht, &id, &iter);
248 node = lttng_ht_iter_get_node_u64(&iter);
249 if (!node) {
250 DBG("Session find by ID %" PRIu64 " id NOT found", id);
251 goto end;
252 }
253 session = caa_container_of(node, struct relay_session, session_n);
254 DBG("Session find by ID %" PRIu64 " id found", id);
255 if (!session_get(session)) {
256 session = NULL;
257 }
258 end:
259 rcu_read_unlock();
260 return session;
261 }
262
263 static void rcu_destroy_session(struct rcu_head *rcu_head)
264 {
265 struct relay_session *session =
266 caa_container_of(rcu_head, struct relay_session,
267 rcu_node);
268 /*
269 * Since each trace has a reference on the session, it means
270 * that if we are at the point where we teardown the session, no
271 * trace belonging to that session exist at this point.
272 * Calling lttng_ht_destroy in call_rcu worker thread so we
273 * don't hold the RCU read-side lock while calling it.
274 */
275 lttng_ht_destroy(session->ctf_traces_ht);
276 free(session);
277 }
278
279 /*
280 * Delete session from the given hash table.
281 *
282 * Return lttng ht del error code being 0 on success and 1 on failure.
283 */
284 static int session_delete(struct relay_session *session)
285 {
286 struct lttng_ht_iter iter;
287
288 iter.iter.node = &session->session_n.node;
289 return lttng_ht_del(sessions_ht, &iter);
290 }
291
292
293 static void destroy_session(struct relay_session *session)
294 {
295 int ret;
296
297 ret = session_delete(session);
298 assert(!ret);
299 lttng_trace_chunk_put(session->current_trace_chunk);
300 session->current_trace_chunk = NULL;
301 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
302 session->pending_closure_trace_chunk = NULL;
303 ret = sessiond_trace_chunk_registry_session_destroyed(
304 sessiond_trace_chunk_registry, session->sessiond_uuid);
305 assert(!ret);
306 call_rcu(&session->rcu_node, rcu_destroy_session);
307 }
308
309 void session_release(struct urcu_ref *ref)
310 {
311 struct relay_session *session =
312 caa_container_of(ref, struct relay_session, ref);
313
314 destroy_session(session);
315 }
316
317 void session_put(struct relay_session *session)
318 {
319 rcu_read_lock();
320 urcu_ref_put(&session->ref, session_release);
321 rcu_read_unlock();
322 }
323
324 int session_close(struct relay_session *session)
325 {
326 int ret = 0;
327 struct ctf_trace *trace;
328 struct lttng_ht_iter iter;
329 struct relay_stream *stream;
330
331 pthread_mutex_lock(&session->lock);
332 DBG("closing session %" PRIu64 ": is conn already closed %d",
333 session->id, session->connection_closed);
334 session->connection_closed = true;
335 pthread_mutex_unlock(&session->lock);
336
337 rcu_read_lock();
338 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
339 &iter.iter, trace, node.node) {
340 ret = ctf_trace_close(trace);
341 if (ret) {
342 goto rcu_unlock;
343 }
344 }
345 cds_list_for_each_entry_rcu(stream, &session->recv_list,
346 recv_node) {
347 /* Close streams which have not been published yet. */
348 try_stream_close(stream);
349 }
350 rcu_unlock:
351 rcu_read_unlock();
352 if (ret) {
353 return ret;
354 }
355 /* Put self-reference from create. */
356 session_put(session);
357 return ret;
358 }
359
360 int session_abort(struct relay_session *session)
361 {
362 int ret = 0;
363
364 if (!session) {
365 return 0;
366 }
367
368 pthread_mutex_lock(&session->lock);
369 DBG("aborting session %" PRIu64, session->id);
370 session->aborted = true;
371 pthread_mutex_unlock(&session->lock);
372 return ret;
373 }
374
375 void print_sessions(void)
376 {
377 struct lttng_ht_iter iter;
378 struct relay_session *session;
379
380 if (!sessions_ht) {
381 return;
382 }
383
384 rcu_read_lock();
385 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
386 session_n.node) {
387 if (!session_get(session)) {
388 continue;
389 }
390 DBG("session %p refcount %ld session %" PRIu64,
391 session,
392 session->ref.refcount,
393 session->id);
394 session_put(session);
395 }
396 rcu_read_unlock();
397 }
This page took 0.037269 seconds and 5 git commands to generate.