Fix: init_session_output_path is valid for peer >= 2.11 only
[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 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
120 end:
121 free(session_directory);
122 return ret;
123 }
124
125 static 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;
131 char *output_path;
132 bool output_path_allocated = false;
133
134 if (!opt_output_path) {
135 /* No output path defined */
136 const char *home_dir = utils_get_home_dir();
137 if (!home_dir) {
138 ERR("Home path not found."
139 " Please specify an output path using -o, --output PATH");
140 ret = -1;
141 goto end;
142 }
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;
153 }
154
155 ret = lttng_directory_handle_init(&output_directory, output_path);
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;
178 end:
179 lttng_trace_chunk_put(chunk);
180 lttng_directory_handle_fini(&output_directory);
181 if (output_path_allocated) {
182 free(output_path);
183 }
184 return ret;
185 }
186
187 /*
188 * Create a new session by assigning a new session ID.
189 *
190 * Return allocated session or else NULL.
191 */
192 struct relay_session *session_create(const char *session_name,
193 const char *hostname, const char *base_path,
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,
201 uint32_t minor,
202 bool session_name_contains_creation_time)
203 {
204 int ret;
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 }
222
223 session = zmalloc(sizeof(*session));
224 if (!session) {
225 PERROR("Failed to allocate session");
226 goto error;
227 }
228 if (lttng_strncpy(session->session_name, session_name,
229 sizeof(session->session_name))) {
230 WARN("Session name exceeds maximal allowed length");
231 goto error;
232 }
233 if (lttng_strncpy(session->hostname, hostname,
234 sizeof(session->hostname))) {
235 WARN("Hostname exceeds maximal allowed length");
236 goto error;
237 }
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 }
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;
248
249 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
250 if (!session->ctf_traces_ht) {
251 goto error;
252 }
253
254 pthread_mutex_lock(&last_relay_session_id_lock);
255 session->id = ++last_relay_session_id;
256 pthread_mutex_unlock(&last_relay_session_id_lock);
257
258 session->major = major;
259 session->minor = minor;
260 lttng_ht_node_init_u64(&session->session_n, session->id);
261 urcu_ref_init(&session->ref);
262 CDS_INIT_LIST_HEAD(&session->recv_list);
263 pthread_mutex_init(&session->lock, NULL);
264 pthread_mutex_init(&session->recv_list_lock, NULL);
265
266 session->live_timer = live_timer;
267 session->snapshot = snapshot;
268 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
269
270 if (id_sessiond) {
271 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
272 }
273
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 }
280 }
281
282 ret = sessiond_trace_chunk_registry_session_created(
283 sessiond_trace_chunk_registry, sessiond_uuid);
284 if (ret) {
285 goto error;
286 }
287
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
315 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
316 return session;
317
318 error:
319 session_put(session);
320 return NULL;
321 }
322
323 /* Should be called with RCU read-side lock held. */
324 bool session_get(struct relay_session *session)
325 {
326 return urcu_ref_get_unless_zero(&session->ref);
327 }
328
329 /*
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.
333 *
334 * Return session or NULL if not found.
335 */
336 struct relay_session *session_get_by_id(uint64_t id)
337 {
338 struct relay_session *session = NULL;
339 struct lttng_ht_node_u64 *node;
340 struct lttng_ht_iter iter;
341
342 rcu_read_lock();
343 lttng_ht_lookup(sessions_ht, &id, &iter);
344 node = lttng_ht_iter_get_node_u64(&iter);
345 if (!node) {
346 DBG("Session find by ID %" PRIu64 " id NOT found", id);
347 goto end;
348 }
349 session = caa_container_of(node, struct relay_session, session_n);
350 DBG("Session find by ID %" PRIu64 " id found", id);
351 if (!session_get(session)) {
352 session = NULL;
353 }
354 end:
355 rcu_read_unlock();
356 return session;
357 }
358
359 static 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);
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);
372 free(session);
373 }
374
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 */
380 static int session_delete(struct relay_session *session)
381 {
382 struct lttng_ht_iter iter;
383
384 iter.iter.node = &session->session_n.node;
385 return lttng_ht_del(sessions_ht, &iter);
386 }
387
388
389 static void destroy_session(struct relay_session *session)
390 {
391 int ret;
392
393 ret = session_delete(session);
394 assert(!ret);
395 lttng_trace_chunk_put(session->current_trace_chunk);
396 session->current_trace_chunk = NULL;
397 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
398 session->pending_closure_trace_chunk = NULL;
399 ret = sessiond_trace_chunk_registry_session_destroyed(
400 sessiond_trace_chunk_registry, session->sessiond_uuid);
401 assert(!ret);
402 call_rcu(&session->rcu_node, rcu_destroy_session);
403 }
404
405 void session_release(struct urcu_ref *ref)
406 {
407 struct relay_session *session =
408 caa_container_of(ref, struct relay_session, ref);
409
410 destroy_session(session);
411 }
412
413 void session_put(struct relay_session *session)
414 {
415 rcu_read_lock();
416 urcu_ref_put(&session->ref, session_release);
417 rcu_read_unlock();
418 }
419
420 int session_close(struct relay_session *session)
421 {
422 int ret = 0;
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);
430 session->connection_closed = true;
431 pthread_mutex_unlock(&session->lock);
432
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;
439 }
440 }
441 cds_list_for_each_entry_rcu(stream, &session->recv_list,
442 recv_node) {
443 /* Close streams which have not been published yet. */
444 try_stream_close(stream);
445 }
446 rcu_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;
454 }
455
456 int 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);
466 session->aborted = true;
467 pthread_mutex_unlock(&session->lock);
468 return ret;
469 }
470
471 void print_sessions(void)
472 {
473 struct lttng_ht_iter iter;
474 struct relay_session *session;
475
476 if (!sessions_ht) {
477 return;
478 }
479
480 rcu_read_lock();
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);
491 }
492 rcu_read_unlock();
493 }
This page took 0.040737 seconds and 6 git commands to generate.