Fix: chunk state is not set when relayd does not support trace chunks
[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
ecd1a12f
MD
274 ret = init_session_output_path(session);
275 if (ret) {
276 goto error;
277 }
23c8ff50
JG
278 ret = sessiond_trace_chunk_registry_session_created(
279 sessiond_trace_chunk_registry, sessiond_uuid);
280 if (ret) {
281 goto error;
282 }
7591bab1 283
1e791a74
JG
284 if (id_sessiond && current_chunk_id) {
285 session->current_trace_chunk =
286 sessiond_trace_chunk_registry_get_chunk(
287 sessiond_trace_chunk_registry,
288 session->sessiond_uuid,
289 session->id_sessiond.value,
290 *current_chunk_id);
291 if (!session->current_trace_chunk) {
292 char uuid_str[UUID_STR_LEN];
293
294 lttng_uuid_to_str(sessiond_uuid, uuid_str);
295 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
296 uuid_str, *id_sessiond,
297 *current_chunk_id);
298 }
299 } else if (!id_sessiond) {
300 /*
301 * Pre-2.11 peers will not announce trace chunks. An
302 * anonymous trace chunk which will remain set for the
303 * duration of the session is created.
304 */
305 ret = session_set_anonymous_chunk(session);
306 if (ret) {
307 goto error;
308 }
309 }
310
7591bab1 311 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
bb5d54e7 312 return session;
2a174661
DG
313
314error:
1e791a74 315 session_put(session);
bb5d54e7 316 return NULL;
2a174661 317}
2f8f53af 318
7591bab1
MD
319/* Should be called with RCU read-side lock held. */
320bool session_get(struct relay_session *session)
321{
ce4d4083 322 return urcu_ref_get_unless_zero(&session->ref);
7591bab1
MD
323}
324
2f8f53af 325/*
7591bab1
MD
326 * Lookup a session within the session hash table using the session id
327 * as key. A session reference is taken when a session is returned.
328 * session_put() must be called on that session.
2f8f53af
DG
329 *
330 * Return session or NULL if not found.
331 */
7591bab1 332struct relay_session *session_get_by_id(uint64_t id)
2f8f53af
DG
333{
334 struct relay_session *session = NULL;
2a174661 335 struct lttng_ht_node_u64 *node;
2f8f53af
DG
336 struct lttng_ht_iter iter;
337
7591bab1
MD
338 rcu_read_lock();
339 lttng_ht_lookup(sessions_ht, &id, &iter);
2a174661 340 node = lttng_ht_iter_get_node_u64(&iter);
2f8f53af 341 if (!node) {
2a174661 342 DBG("Session find by ID %" PRIu64 " id NOT found", id);
2f8f53af
DG
343 goto end;
344 }
345 session = caa_container_of(node, struct relay_session, session_n);
2a174661 346 DBG("Session find by ID %" PRIu64 " id found", id);
7591bab1
MD
347 if (!session_get(session)) {
348 session = NULL;
349 }
2f8f53af 350end:
7591bab1 351 rcu_read_unlock();
2f8f53af
DG
352 return session;
353}
2a174661 354
7591bab1
MD
355static void rcu_destroy_session(struct rcu_head *rcu_head)
356{
357 struct relay_session *session =
358 caa_container_of(rcu_head, struct relay_session,
359 rcu_node);
49e614cb
MD
360 /*
361 * Since each trace has a reference on the session, it means
362 * that if we are at the point where we teardown the session, no
363 * trace belonging to that session exist at this point.
364 * Calling lttng_ht_destroy in call_rcu worker thread so we
365 * don't hold the RCU read-side lock while calling it.
366 */
367 lttng_ht_destroy(session->ctf_traces_ht);
7591bab1
MD
368 free(session);
369}
370
2a174661
DG
371/*
372 * Delete session from the given hash table.
373 *
374 * Return lttng ht del error code being 0 on success and 1 on failure.
375 */
7591bab1 376static int session_delete(struct relay_session *session)
2a174661
DG
377{
378 struct lttng_ht_iter iter;
379
2a174661 380 iter.iter.node = &session->session_n.node;
7591bab1 381 return lttng_ht_del(sessions_ht, &iter);
2a174661
DG
382}
383
7591bab1
MD
384
385static void destroy_session(struct relay_session *session)
386{
387 int ret;
388
389 ret = session_delete(session);
390 assert(!ret);
639ddf68 391 lttng_trace_chunk_put(session->current_trace_chunk);
c35f9726 392 session->current_trace_chunk = NULL;
62bad3bf
JG
393 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
394 session->pending_closure_trace_chunk = NULL;
23c8ff50
JG
395 ret = sessiond_trace_chunk_registry_session_destroyed(
396 sessiond_trace_chunk_registry, session->sessiond_uuid);
397 assert(!ret);
7591bab1
MD
398 call_rcu(&session->rcu_node, rcu_destroy_session);
399}
400
401void session_release(struct urcu_ref *ref)
2a174661 402{
7591bab1
MD
403 struct relay_session *session =
404 caa_container_of(ref, struct relay_session, ref);
2a174661 405
7591bab1
MD
406 destroy_session(session);
407}
2a174661 408
7591bab1
MD
409void session_put(struct relay_session *session)
410{
411 rcu_read_lock();
7591bab1 412 urcu_ref_put(&session->ref, session_release);
7591bab1 413 rcu_read_unlock();
2a174661
DG
414}
415
7591bab1 416int session_close(struct relay_session *session)
2a174661
DG
417{
418 int ret = 0;
7591bab1
MD
419 struct ctf_trace *trace;
420 struct lttng_ht_iter iter;
421 struct relay_stream *stream;
422
423 pthread_mutex_lock(&session->lock);
424 DBG("closing session %" PRIu64 ": is conn already closed %d",
425 session->id, session->connection_closed);
7591bab1 426 session->connection_closed = true;
7591bab1 427 pthread_mutex_unlock(&session->lock);
2a174661 428
7591bab1
MD
429 rcu_read_lock();
430 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
431 &iter.iter, trace, node.node) {
432 ret = ctf_trace_close(trace);
433 if (ret) {
434 goto rcu_unlock;
2a174661
DG
435 }
436 }
7591bab1
MD
437 cds_list_for_each_entry_rcu(stream, &session->recv_list,
438 recv_node) {
bda7c7b9
JG
439 /* Close streams which have not been published yet. */
440 try_stream_close(stream);
7591bab1
MD
441 }
442rcu_unlock:
443 rcu_read_unlock();
444 if (ret) {
445 return ret;
446 }
447 /* Put self-reference from create. */
448 session_put(session);
449 return ret;
2a174661
DG
450}
451
98ba050e
JR
452int session_abort(struct relay_session *session)
453{
454 int ret = 0;
455
456 if (!session) {
457 return 0;
458 }
459
460 pthread_mutex_lock(&session->lock);
461 DBG("aborting session %" PRIu64, session->id);
98ba050e 462 session->aborted = true;
98ba050e
JR
463 pthread_mutex_unlock(&session->lock);
464 return ret;
465}
466
7591bab1 467void print_sessions(void)
2a174661 468{
2a174661 469 struct lttng_ht_iter iter;
7591bab1 470 struct relay_session *session;
2a174661 471
ce3f3ba3
JG
472 if (!sessions_ht) {
473 return;
474 }
475
2a174661 476 rcu_read_lock();
7591bab1
MD
477 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
478 session_n.node) {
479 if (!session_get(session)) {
480 continue;
481 }
482 DBG("session %p refcount %ld session %" PRIu64,
483 session,
484 session->ref.refcount,
485 session->id);
486 session_put(session);
2a174661 487 }
2a174661 488 rcu_read_unlock();
2a174661 489}
This page took 0.071319 seconds and 5 git commands to generate.