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