Fix: init_session_output_path is valid for peer >= 2.11 only
[lttng-tools.git] / src / bin / lttng-relayd / session.c
CommitLineData
2f8f53af
DG
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
7591bab1 4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2f8f53af
DG
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
6c1c0768 20#define _LGPL_SOURCE
2a174661 21#include <common/common.h>
a620c891 22#include <common/utils.h>
1e791a74 23#include <common/compat/uuid.h>
7591bab1 24#include <urcu/rculist.h>
2a174661
DG
25
26#include "ctf-trace.h"
a620c891 27#include "lttng-relayd.h"
2f8f53af 28#include "session.h"
23c8ff50 29#include "sessiond-trace-chunks.h"
a620c891 30#include "stream.h"
a0409c33 31#include <common/defaults.h>
2a174661
DG
32
33/* Global session id used in the session creation. */
34static uint64_t last_relay_session_id;
7591bab1 35static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
2a174661 36
ecd1a12f
MD
37static int init_session_output_path(struct relay_session *session)
38{
39 /*
40 * session_directory:
41 *
42 * if base_path is \0'
43 * hostname/session_name
44 * else
45 * hostname/base_path
46 */
47 char *session_directory = NULL;
48 int ret = 0;
49
50 if (session->output_path[0] != '\0') {
51 goto end;
52 }
53 /*
54 * If base path is set, it overrides the session name for the
55 * session relative base path. No timestamp is appended if the
56 * base path is overridden.
57 *
58 * If the session name already contains the creation time (e.g.
59 * auto-<timestamp>, don't append yet another timestamp after
60 * the session name in the generated path.
61 *
62 * Otherwise, generate the path with session_name-<timestamp>.
63 */
64 if (session->base_path[0] != '\0') {
65 ret = asprintf(&session_directory, "%s/%s", session->hostname,
66 session->base_path);
67 } else if (session->session_name_contains_creation_time) {
68 ret = asprintf(&session_directory, "%s/%s", session->hostname,
69 session->session_name);
70 } else {
71 char session_creation_datetime[16];
72 size_t strftime_ret;
73 struct tm *timeinfo;
74 time_t creation_time;
75
76 /*
77 * The 2.11+ protocol guarantees that a creation time
78 * is provided for a session. This would indicate a
79 * protocol error or an improper use of this util.
80 */
81 if (!session->creation_time.is_set) {
82 ERR("Creation time missing for session \"%s\" (protocol error)",
83 session->session_name);
84 ret = -1;
85 goto end;
86 }
87 creation_time = LTTNG_OPTIONAL_GET(session->creation_time);
88
89 timeinfo = localtime(&creation_time);
90 if (!timeinfo) {
91 ERR("Failed to get timeinfo while initializing session output directory handle");
92 ret = -1;
93 goto end;
94 }
95 strftime_ret = strftime(session_creation_datetime,
96 sizeof(session_creation_datetime),
97 "%Y%m%d-%H%M%S", timeinfo);
98 if (strftime_ret == 0) {
99 ERR("Failed to format session creation timestamp while initializing session output directory handle");
100 ret = -1;
101 goto end;
102 }
103 ret = asprintf(&session_directory, "%s/%s-%s",
104 session->hostname, session->session_name,
105 session_creation_datetime);
106 }
107 if (ret < 0) {
108 PERROR("Failed to format session directory name");
109 goto end;
110 }
111
112 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
113 ERR("Session output directory exceeds maximal length");
114 ret = -1;
115 goto end;
116 }
117 strcpy(session->output_path, session_directory);
118 ret = 0;
119
120end:
121 free(session_directory);
122 return ret;
123}
124
1e791a74
JG
125static int session_set_anonymous_chunk(struct relay_session *session)
126{
127 int ret = 0;
128 struct lttng_trace_chunk *chunk = NULL;
129 enum lttng_trace_chunk_status status;
130 struct lttng_directory_handle output_directory;
a0409c33
MD
131 char *output_path;
132 bool output_path_allocated = false;
a620c891 133
a0409c33 134 if (!opt_output_path) {
a620c891 135 /* No output path defined */
a0409c33
MD
136 const char *home_dir = utils_get_home_dir();
137 if (!home_dir) {
334dfcb7
JG
138 ERR("Home path not found."
139 " Please specify an output path using -o, --output PATH");
a620c891
JR
140 ret = -1;
141 goto end;
142 }
a0409c33
MD
143 ret = asprintf(&output_path, "%s/%s", home_dir, DEFAULT_TRACE_DIR_NAME);
144 if (ret < 0) {
145 PERROR("asprintf trace dir name");
146 ret = -1;
147 goto end;
148 }
149 output_path_allocated = true;
150 } else {
151 output_path = opt_output_path;
152 output_path_allocated = false;
a620c891 153 }
1e791a74 154
a0409c33 155 ret = lttng_directory_handle_init(&output_directory, output_path);
1e791a74
JG
156 if (ret) {
157 goto end;
158 }
159
160 chunk = lttng_trace_chunk_create_anonymous();
161 if (!chunk) {
162 goto end;
163 }
164
165 status = lttng_trace_chunk_set_credentials_current_user(chunk);
166 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
167 ret = -1;
168 goto end;
169 }
170
171 status = lttng_trace_chunk_set_as_owner(chunk, &output_directory);
172 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
173 ret = -1;
174 goto end;
175 }
176 session->current_trace_chunk = chunk;
177 chunk = NULL;
178end:
179 lttng_trace_chunk_put(chunk);
180 lttng_directory_handle_fini(&output_directory);
a0409c33
MD
181 if (output_path_allocated) {
182 free(output_path);
183 }
1e791a74
JG
184 return ret;
185}
186
2a174661
DG
187/*
188 * Create a new session by assigning a new session ID.
189 *
190 * Return allocated session or else NULL.
191 */
7591bab1 192struct relay_session *session_create(const char *session_name,
6fa5fe7c 193 const char *hostname, const char *base_path,
db1da059
JG
194 uint32_t live_timer,
195 bool snapshot,
196 const lttng_uuid sessiond_uuid,
197 const uint64_t *id_sessiond,
198 const uint64_t *current_chunk_id,
199 const time_t *creation_time,
200 uint32_t major,
46ef2188
MD
201 uint32_t minor,
202 bool session_name_contains_creation_time)
2a174661 203{
23c8ff50 204 int ret;
590f0324
JG
205 struct relay_session *session = NULL;
206
207 if (session_name && strstr(session_name, ".")) {
208 ERR("Illegal character in session name: \"%s\"",
209 session_name);
210 goto error;
211 }
212 if (base_path && strstr(base_path, "../")) {
213 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
214 base_path);
215 goto error;
216 }
217 if (hostname && strstr(hostname, ".")) {
218 ERR("Invalid character in hostname: \"%s\"",
219 hostname);
220 goto error;
221 }
2a174661
DG
222
223 session = zmalloc(sizeof(*session));
224 if (!session) {
1e791a74 225 PERROR("Failed to allocate session");
2a174661
DG
226 goto error;
227 }
bb5d54e7
MD
228 if (lttng_strncpy(session->session_name, session_name,
229 sizeof(session->session_name))) {
1e791a74 230 WARN("Session name exceeds maximal allowed length");
bb5d54e7
MD
231 goto error;
232 }
233 if (lttng_strncpy(session->hostname, hostname,
234 sizeof(session->hostname))) {
1e791a74 235 WARN("Hostname exceeds maximal allowed length");
bb5d54e7
MD
236 goto error;
237 }
6fa5fe7c
MD
238 if (lttng_strncpy(session->base_path, base_path,
239 sizeof(session->base_path))) {
240 WARN("Base path exceeds maximal allowed length");
241 goto error;
242 }
46ef2188
MD
243 if (creation_time) {
244 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
245 }
246 session->session_name_contains_creation_time =
247 session_name_contains_creation_time;
6fa5fe7c 248
2a174661
DG
249 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
250 if (!session->ctf_traces_ht) {
2a174661
DG
251 goto error;
252 }
253
7591bab1 254 pthread_mutex_lock(&last_relay_session_id_lock);
2a174661 255 session->id = ++last_relay_session_id;
7591bab1
MD
256 pthread_mutex_unlock(&last_relay_session_id_lock);
257
258 session->major = major;
259 session->minor = minor;
2a174661 260 lttng_ht_node_init_u64(&session->session_n, session->id);
7591bab1
MD
261 urcu_ref_init(&session->ref);
262 CDS_INIT_LIST_HEAD(&session->recv_list);
263 pthread_mutex_init(&session->lock, NULL);
7591bab1
MD
264 pthread_mutex_init(&session->recv_list_lock, NULL);
265
7591bab1
MD
266 session->live_timer = live_timer;
267 session->snapshot = snapshot;
23c8ff50
JG
268 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
269
1e791a74
JG
270 if (id_sessiond) {
271 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
272 }
273
d519f442
JR
274 if (major == 2 && minor >= 11) {
275 /* Only applies for 2.11+ peers using trace chunks. */
276 ret = init_session_output_path(session);
277 if (ret) {
278 goto error;
279 }
ecd1a12f 280 }
d519f442 281
23c8ff50
JG
282 ret = sessiond_trace_chunk_registry_session_created(
283 sessiond_trace_chunk_registry, sessiond_uuid);
284 if (ret) {
285 goto error;
286 }
7591bab1 287
1e791a74
JG
288 if (id_sessiond && current_chunk_id) {
289 session->current_trace_chunk =
290 sessiond_trace_chunk_registry_get_chunk(
291 sessiond_trace_chunk_registry,
292 session->sessiond_uuid,
293 session->id_sessiond.value,
294 *current_chunk_id);
295 if (!session->current_trace_chunk) {
296 char uuid_str[UUID_STR_LEN];
297
298 lttng_uuid_to_str(sessiond_uuid, uuid_str);
299 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
300 uuid_str, *id_sessiond,
301 *current_chunk_id);
302 }
303 } else if (!id_sessiond) {
304 /*
305 * Pre-2.11 peers will not announce trace chunks. An
306 * anonymous trace chunk which will remain set for the
307 * duration of the session is created.
308 */
309 ret = session_set_anonymous_chunk(session);
310 if (ret) {
311 goto error;
312 }
313 }
314
7591bab1 315 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
bb5d54e7 316 return session;
2a174661
DG
317
318error:
1e791a74 319 session_put(session);
bb5d54e7 320 return NULL;
2a174661 321}
2f8f53af 322
7591bab1
MD
323/* Should be called with RCU read-side lock held. */
324bool session_get(struct relay_session *session)
325{
ce4d4083 326 return urcu_ref_get_unless_zero(&session->ref);
7591bab1
MD
327}
328
2f8f53af 329/*
7591bab1
MD
330 * Lookup a session within the session hash table using the session id
331 * as key. A session reference is taken when a session is returned.
332 * session_put() must be called on that session.
2f8f53af
DG
333 *
334 * Return session or NULL if not found.
335 */
7591bab1 336struct relay_session *session_get_by_id(uint64_t id)
2f8f53af
DG
337{
338 struct relay_session *session = NULL;
2a174661 339 struct lttng_ht_node_u64 *node;
2f8f53af
DG
340 struct lttng_ht_iter iter;
341
7591bab1
MD
342 rcu_read_lock();
343 lttng_ht_lookup(sessions_ht, &id, &iter);
2a174661 344 node = lttng_ht_iter_get_node_u64(&iter);
2f8f53af 345 if (!node) {
2a174661 346 DBG("Session find by ID %" PRIu64 " id NOT found", id);
2f8f53af
DG
347 goto end;
348 }
349 session = caa_container_of(node, struct relay_session, session_n);
2a174661 350 DBG("Session find by ID %" PRIu64 " id found", id);
7591bab1
MD
351 if (!session_get(session)) {
352 session = NULL;
353 }
2f8f53af 354end:
7591bab1 355 rcu_read_unlock();
2f8f53af
DG
356 return session;
357}
2a174661 358
7591bab1
MD
359static void rcu_destroy_session(struct rcu_head *rcu_head)
360{
361 struct relay_session *session =
362 caa_container_of(rcu_head, struct relay_session,
363 rcu_node);
49e614cb
MD
364 /*
365 * Since each trace has a reference on the session, it means
366 * that if we are at the point where we teardown the session, no
367 * trace belonging to that session exist at this point.
368 * Calling lttng_ht_destroy in call_rcu worker thread so we
369 * don't hold the RCU read-side lock while calling it.
370 */
371 lttng_ht_destroy(session->ctf_traces_ht);
7591bab1
MD
372 free(session);
373}
374
2a174661
DG
375/*
376 * Delete session from the given hash table.
377 *
378 * Return lttng ht del error code being 0 on success and 1 on failure.
379 */
7591bab1 380static int session_delete(struct relay_session *session)
2a174661
DG
381{
382 struct lttng_ht_iter iter;
383
2a174661 384 iter.iter.node = &session->session_n.node;
7591bab1 385 return lttng_ht_del(sessions_ht, &iter);
2a174661
DG
386}
387
7591bab1
MD
388
389static void destroy_session(struct relay_session *session)
390{
391 int ret;
392
393 ret = session_delete(session);
394 assert(!ret);
639ddf68 395 lttng_trace_chunk_put(session->current_trace_chunk);
c35f9726 396 session->current_trace_chunk = NULL;
62bad3bf
JG
397 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
398 session->pending_closure_trace_chunk = NULL;
23c8ff50
JG
399 ret = sessiond_trace_chunk_registry_session_destroyed(
400 sessiond_trace_chunk_registry, session->sessiond_uuid);
401 assert(!ret);
7591bab1
MD
402 call_rcu(&session->rcu_node, rcu_destroy_session);
403}
404
405void session_release(struct urcu_ref *ref)
2a174661 406{
7591bab1
MD
407 struct relay_session *session =
408 caa_container_of(ref, struct relay_session, ref);
2a174661 409
7591bab1
MD
410 destroy_session(session);
411}
2a174661 412
7591bab1
MD
413void session_put(struct relay_session *session)
414{
415 rcu_read_lock();
7591bab1 416 urcu_ref_put(&session->ref, session_release);
7591bab1 417 rcu_read_unlock();
2a174661
DG
418}
419
7591bab1 420int session_close(struct relay_session *session)
2a174661
DG
421{
422 int ret = 0;
7591bab1
MD
423 struct ctf_trace *trace;
424 struct lttng_ht_iter iter;
425 struct relay_stream *stream;
426
427 pthread_mutex_lock(&session->lock);
428 DBG("closing session %" PRIu64 ": is conn already closed %d",
429 session->id, session->connection_closed);
7591bab1 430 session->connection_closed = true;
7591bab1 431 pthread_mutex_unlock(&session->lock);
2a174661 432
7591bab1
MD
433 rcu_read_lock();
434 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
435 &iter.iter, trace, node.node) {
436 ret = ctf_trace_close(trace);
437 if (ret) {
438 goto rcu_unlock;
2a174661
DG
439 }
440 }
7591bab1
MD
441 cds_list_for_each_entry_rcu(stream, &session->recv_list,
442 recv_node) {
bda7c7b9
JG
443 /* Close streams which have not been published yet. */
444 try_stream_close(stream);
7591bab1
MD
445 }
446rcu_unlock:
447 rcu_read_unlock();
448 if (ret) {
449 return ret;
450 }
451 /* Put self-reference from create. */
452 session_put(session);
453 return ret;
2a174661
DG
454}
455
98ba050e
JR
456int session_abort(struct relay_session *session)
457{
458 int ret = 0;
459
460 if (!session) {
461 return 0;
462 }
463
464 pthread_mutex_lock(&session->lock);
465 DBG("aborting session %" PRIu64, session->id);
98ba050e 466 session->aborted = true;
98ba050e
JR
467 pthread_mutex_unlock(&session->lock);
468 return ret;
469}
470
7591bab1 471void print_sessions(void)
2a174661 472{
2a174661 473 struct lttng_ht_iter iter;
7591bab1 474 struct relay_session *session;
2a174661 475
ce3f3ba3
JG
476 if (!sessions_ht) {
477 return;
478 }
479
2a174661 480 rcu_read_lock();
7591bab1
MD
481 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
482 session_n.node) {
483 if (!session_get(session)) {
484 continue;
485 }
486 DBG("session %p refcount %ld session %" PRIu64,
487 session,
488 session->ref.refcount,
489 session->id);
490 session_put(session);
2a174661 491 }
2a174661 492 rcu_read_unlock();
2a174661 493}
This page took 0.094796 seconds and 5 git commands to generate.