8f61e715fe5af8490f2cc8c5e7822a75e3d3bbde
[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/uuid.h>
23 #include <common/time.h>
24 #include <common/utils.h>
25 #include <common/uuid.h>
26 #include <urcu/rculist.h>
27
28 #include <sys/stat.h>
29
30 #include "ctf-trace.h"
31 #include "lttng-relayd.h"
32 #include "session.h"
33 #include "sessiond-trace-chunks.h"
34 #include "stream.h"
35 #include <common/defaults.h>
36 #include "utils.h"
37
38 /* Global session id used in the session creation. */
39 static uint64_t last_relay_session_id;
40 static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
41
42 static int init_session_output_path_group_by_host(struct relay_session *session)
43 {
44 /*
45 * session_directory:
46 *
47 * if base_path is \0'
48 * hostname/session_name
49 * else
50 * hostname/base_path
51 */
52 char *session_directory = NULL;
53 int ret = 0;
54
55 if (session->output_path[0] != '\0') {
56 goto end;
57 }
58 /*
59 * If base path is set, it overrides the session name for the
60 * session relative base path. No timestamp is appended if the
61 * base path is overridden.
62 *
63 * If the session name already contains the creation time (e.g.
64 * auto-<timestamp>, don't append yet another timestamp after
65 * the session name in the generated path.
66 *
67 * Otherwise, generate the path with session_name-<timestamp>.
68 */
69 if (session->base_path[0] != '\0') {
70 ret = asprintf(&session_directory, "%s/%s", session->hostname,
71 session->base_path);
72 } else if (session->session_name_contains_creation_time) {
73 ret = asprintf(&session_directory, "%s/%s", session->hostname,
74 session->session_name);
75 } else {
76 char session_creation_datetime[DATETIME_STR_LEN];
77
78 ret = time_to_datetime_str(
79 LTTNG_OPTIONAL_GET(session->creation_time),
80 session_creation_datetime,
81 sizeof(session_creation_datetime));
82 if (ret) {
83 ERR("Failed to format session creation timestamp while initializing session output directory handle");
84 ret = -1;
85 goto end;
86 }
87
88 ret = asprintf(&session_directory, "%s/%s-%s",
89 session->hostname, session->session_name,
90 session_creation_datetime);
91 }
92 if (ret < 0) {
93 PERROR("Failed to format session directory name");
94 goto end;
95 }
96
97 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
98 ERR("Session output directory exceeds maximal length");
99 ret = -1;
100 goto end;
101 }
102 strcpy(session->output_path, session_directory);
103 ret = 0;
104
105 end:
106 free(session_directory);
107 return ret;
108 }
109
110 static int init_session_output_path_group_by_session(
111 struct relay_session *session)
112 {
113 /*
114 * session_directory:
115 *
116 * session_name/hostname-creation_time/base_path
117 *
118 * For session name including the datetime, use it as the complete name
119 * since. Do not perform modification on it since the datetime is an
120 * integral part of the name and how a user identify a session.
121 */
122 int ret = 0;
123 char *session_directory = NULL;
124 char creation_datetime[DATETIME_STR_LEN];
125
126 if (session->output_path[0] != '\0') {
127 /* output_path as been generated already */
128 goto end;
129 }
130
131 ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time),
132 creation_datetime, sizeof(creation_datetime));
133 if (ret) {
134 ERR("Failed to format session creation timestamp while initializing session output directory handle");
135 ret = -1;
136 goto end;
137 }
138
139 ret = asprintf(&session_directory, "%s/%s-%s%s%s",
140 session->session_name, session->hostname,
141 creation_datetime,
142 session->base_path[0] != '\0' ? "/" : "",
143 session->base_path);
144 if (ret < 0) {
145 PERROR("Failed to format session directory name");
146 goto end;
147 }
148
149 if (strlen(session_directory) >= LTTNG_PATH_MAX) {
150 ERR("Session output directory exceeds maximal length");
151 ret = -1;
152 goto end;
153 }
154
155 strcpy(session->output_path, session_directory);
156 ret = 0;
157
158 end:
159 free(session_directory);
160 return ret;
161 }
162
163 static int init_session_output_path(struct relay_session *session)
164 {
165 int ret;
166
167 switch (opt_group_output_by) {
168 case RELAYD_GROUP_OUTPUT_BY_HOST:
169 ret = init_session_output_path_group_by_host(session);
170 break;
171 case RELAYD_GROUP_OUTPUT_BY_SESSION:
172 ret = init_session_output_path_group_by_session(session);
173 break;
174 case RELAYD_GROUP_OUTPUT_BY_UNKNOWN:
175 default:
176 abort();
177 break;
178 }
179
180 return ret;
181 }
182
183 static int session_set_anonymous_chunk(struct relay_session *session)
184 {
185 int ret = 0;
186 struct lttng_trace_chunk *chunk = NULL;
187 enum lttng_trace_chunk_status status;
188 struct lttng_directory_handle output_directory;
189
190 ret = session_init_output_directory_handle(session, &output_directory);
191 if (ret) {
192 goto end;
193 }
194
195 chunk = lttng_trace_chunk_create_anonymous();
196 if (!chunk) {
197 goto end;
198 }
199
200 status = lttng_trace_chunk_set_credentials_current_user(chunk);
201 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
202 ret = -1;
203 goto end;
204 }
205
206 status = lttng_trace_chunk_set_as_owner(chunk, &output_directory);
207 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
208 ret = -1;
209 goto end;
210 }
211 session->current_trace_chunk = chunk;
212 chunk = NULL;
213 end:
214 lttng_trace_chunk_put(chunk);
215 lttng_directory_handle_fini(&output_directory);
216 return ret;
217 }
218
219 /*
220 * Create a new session by assigning a new session ID.
221 *
222 * Return allocated session or else NULL.
223 */
224 struct relay_session *session_create(const char *session_name,
225 const char *hostname, const char *base_path,
226 uint32_t live_timer,
227 bool snapshot,
228 const lttng_uuid sessiond_uuid,
229 const uint64_t *id_sessiond,
230 const uint64_t *current_chunk_id,
231 const time_t *creation_time,
232 uint32_t major,
233 uint32_t minor,
234 bool session_name_contains_creation_time)
235 {
236 int ret;
237 struct relay_session *session = NULL;
238
239 assert(session_name);
240 assert(hostname);
241 assert(base_path);
242
243 if (strstr(session_name, ".")) {
244 ERR("Illegal character in session name: \"%s\"",
245 session_name);
246 goto error;
247 }
248 if (strstr(base_path, "../")) {
249 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
250 base_path);
251 goto error;
252 }
253 if (strstr(hostname, ".")) {
254 ERR("Invalid character in hostname: \"%s\"",
255 hostname);
256 goto error;
257 }
258
259 session = zmalloc(sizeof(*session));
260 if (!session) {
261 PERROR("Failed to allocate session");
262 goto error;
263 }
264 if (lttng_strncpy(session->session_name, session_name,
265 sizeof(session->session_name))) {
266 WARN("Session name exceeds maximal allowed length");
267 goto error;
268 }
269 if (lttng_strncpy(session->hostname, hostname,
270 sizeof(session->hostname))) {
271 WARN("Hostname exceeds maximal allowed length");
272 goto error;
273 }
274 if (lttng_strncpy(session->base_path, base_path,
275 sizeof(session->base_path))) {
276 WARN("Base path exceeds maximal allowed length");
277 goto error;
278 }
279 if (creation_time) {
280 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
281 }
282 session->session_name_contains_creation_time =
283 session_name_contains_creation_time;
284
285 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
286 if (!session->ctf_traces_ht) {
287 goto error;
288 }
289
290 pthread_mutex_lock(&last_relay_session_id_lock);
291 session->id = ++last_relay_session_id;
292 pthread_mutex_unlock(&last_relay_session_id_lock);
293
294 session->major = major;
295 session->minor = minor;
296 lttng_ht_node_init_u64(&session->session_n, session->id);
297 urcu_ref_init(&session->ref);
298 CDS_INIT_LIST_HEAD(&session->recv_list);
299 pthread_mutex_init(&session->lock, NULL);
300 pthread_mutex_init(&session->recv_list_lock, NULL);
301
302 session->live_timer = live_timer;
303 session->snapshot = snapshot;
304 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
305
306 if (id_sessiond) {
307 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
308 }
309
310 if (major == 2 && minor >= 11) {
311 /* Only applies for 2.11+ peers using trace chunks. */
312 ret = init_session_output_path(session);
313 if (ret) {
314 goto error;
315 }
316 }
317
318 ret = sessiond_trace_chunk_registry_session_created(
319 sessiond_trace_chunk_registry, sessiond_uuid);
320 if (ret) {
321 goto error;
322 }
323
324 if (id_sessiond && current_chunk_id) {
325 session->current_trace_chunk =
326 sessiond_trace_chunk_registry_get_chunk(
327 sessiond_trace_chunk_registry,
328 session->sessiond_uuid,
329 session->id_sessiond.value,
330 *current_chunk_id);
331 if (!session->current_trace_chunk) {
332 char uuid_str[LTTNG_UUID_STR_LEN];
333
334 lttng_uuid_to_str(sessiond_uuid, uuid_str);
335 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
336 uuid_str, *id_sessiond,
337 *current_chunk_id);
338 }
339 } else if (!id_sessiond) {
340 /*
341 * Pre-2.11 peers will not announce trace chunks. An
342 * anonymous trace chunk which will remain set for the
343 * duration of the session is created.
344 */
345 ret = session_set_anonymous_chunk(session);
346 if (ret) {
347 goto error;
348 }
349 }
350
351 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
352 return session;
353
354 error:
355 session_put(session);
356 return NULL;
357 }
358
359 /* Should be called with RCU read-side lock held. */
360 bool session_get(struct relay_session *session)
361 {
362 return urcu_ref_get_unless_zero(&session->ref);
363 }
364
365 /*
366 * Lookup a session within the session hash table using the session id
367 * as key. A session reference is taken when a session is returned.
368 * session_put() must be called on that session.
369 *
370 * Return session or NULL if not found.
371 */
372 struct relay_session *session_get_by_id(uint64_t id)
373 {
374 struct relay_session *session = NULL;
375 struct lttng_ht_node_u64 *node;
376 struct lttng_ht_iter iter;
377
378 rcu_read_lock();
379 lttng_ht_lookup(sessions_ht, &id, &iter);
380 node = lttng_ht_iter_get_node_u64(&iter);
381 if (!node) {
382 DBG("Session find by ID %" PRIu64 " id NOT found", id);
383 goto end;
384 }
385 session = caa_container_of(node, struct relay_session, session_n);
386 DBG("Session find by ID %" PRIu64 " id found", id);
387 if (!session_get(session)) {
388 session = NULL;
389 }
390 end:
391 rcu_read_unlock();
392 return session;
393 }
394
395 static void rcu_destroy_session(struct rcu_head *rcu_head)
396 {
397 struct relay_session *session =
398 caa_container_of(rcu_head, struct relay_session,
399 rcu_node);
400 /*
401 * Since each trace has a reference on the session, it means
402 * that if we are at the point where we teardown the session, no
403 * trace belonging to that session exist at this point.
404 * Calling lttng_ht_destroy in call_rcu worker thread so we
405 * don't hold the RCU read-side lock while calling it.
406 */
407 lttng_ht_destroy(session->ctf_traces_ht);
408 free(session);
409 }
410
411 /*
412 * Delete session from the given hash table.
413 *
414 * Return lttng ht del error code being 0 on success and 1 on failure.
415 */
416 static int session_delete(struct relay_session *session)
417 {
418 struct lttng_ht_iter iter;
419
420 iter.iter.node = &session->session_n.node;
421 return lttng_ht_del(sessions_ht, &iter);
422 }
423
424
425 static void destroy_session(struct relay_session *session)
426 {
427 int ret;
428
429 ret = session_delete(session);
430 assert(!ret);
431 lttng_trace_chunk_put(session->current_trace_chunk);
432 session->current_trace_chunk = NULL;
433 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
434 session->pending_closure_trace_chunk = NULL;
435 ret = sessiond_trace_chunk_registry_session_destroyed(
436 sessiond_trace_chunk_registry, session->sessiond_uuid);
437 assert(!ret);
438 call_rcu(&session->rcu_node, rcu_destroy_session);
439 }
440
441 static void session_release(struct urcu_ref *ref)
442 {
443 struct relay_session *session =
444 caa_container_of(ref, struct relay_session, ref);
445
446 destroy_session(session);
447 }
448
449 void session_put(struct relay_session *session)
450 {
451 if (!session) {
452 return;
453 }
454 rcu_read_lock();
455 urcu_ref_put(&session->ref, session_release);
456 rcu_read_unlock();
457 }
458
459 int session_close(struct relay_session *session)
460 {
461 int ret = 0;
462 struct ctf_trace *trace;
463 struct lttng_ht_iter iter;
464 struct relay_stream *stream;
465
466 pthread_mutex_lock(&session->lock);
467 DBG("closing session %" PRIu64 ": is conn already closed %d",
468 session->id, session->connection_closed);
469 session->connection_closed = true;
470 pthread_mutex_unlock(&session->lock);
471
472 rcu_read_lock();
473 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
474 &iter.iter, trace, node.node) {
475 ret = ctf_trace_close(trace);
476 if (ret) {
477 goto rcu_unlock;
478 }
479 }
480 cds_list_for_each_entry_rcu(stream, &session->recv_list,
481 recv_node) {
482 /* Close streams which have not been published yet. */
483 try_stream_close(stream);
484 }
485 rcu_unlock:
486 rcu_read_unlock();
487 if (ret) {
488 return ret;
489 }
490 /* Put self-reference from create. */
491 session_put(session);
492 return ret;
493 }
494
495 int session_abort(struct relay_session *session)
496 {
497 int ret = 0;
498
499 if (!session) {
500 return 0;
501 }
502
503 pthread_mutex_lock(&session->lock);
504 DBG("aborting session %" PRIu64, session->id);
505 session->aborted = true;
506 pthread_mutex_unlock(&session->lock);
507 return ret;
508 }
509
510 void print_sessions(void)
511 {
512 struct lttng_ht_iter iter;
513 struct relay_session *session;
514
515 if (!sessions_ht) {
516 return;
517 }
518
519 rcu_read_lock();
520 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
521 session_n.node) {
522 if (!session_get(session)) {
523 continue;
524 }
525 DBG("session %p refcount %ld session %" PRIu64,
526 session,
527 session->ref.refcount,
528 session->id);
529 session_put(session);
530 }
531 rcu_read_unlock();
532 }
533
534 int session_init_output_directory_handle(struct relay_session *session,
535 struct lttng_directory_handle *handle)
536 {
537 int ret;
538 /*
539 * relayd_output_path/session_directory
540 * e.g. /home/user/lttng-traces/hostname/session_name
541 */
542 char *full_session_path = NULL;
543
544 pthread_mutex_lock(&session->lock);
545 full_session_path = create_output_path(session->output_path);
546 if (!full_session_path) {
547 ret = -1;
548 goto end;
549 }
550
551 ret = utils_mkdir_recursive(
552 full_session_path, S_IRWXU | S_IRWXG, -1, -1);
553 if (ret) {
554 ERR("Failed to create session output path \"%s\"",
555 full_session_path);
556 goto end;
557 }
558
559 ret = lttng_directory_handle_init(handle, full_session_path);
560 if (ret) {
561 goto end;
562 }
563 end:
564 pthread_mutex_unlock(&session->lock);
565 free(full_session_path);
566 return ret;
567 }
This page took 0.040976 seconds and 4 git commands to generate.