31f27184f5e1abeefb0908dcb330b23d138b9c09
[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 ret = init_session_output_path(session);
275 if (ret) {
276 goto error;
277 }
278 ret = sessiond_trace_chunk_registry_session_created(
279 sessiond_trace_chunk_registry, sessiond_uuid);
280 if (ret) {
281 goto error;
282 }
283
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
311 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
312 return session;
313
314 error:
315 session_put(session);
316 return NULL;
317 }
318
319 /* Should be called with RCU read-side lock held. */
320 bool session_get(struct relay_session *session)
321 {
322 return urcu_ref_get_unless_zero(&session->ref);
323 }
324
325 /*
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.
329 *
330 * Return session or NULL if not found.
331 */
332 struct relay_session *session_get_by_id(uint64_t id)
333 {
334 struct relay_session *session = NULL;
335 struct lttng_ht_node_u64 *node;
336 struct lttng_ht_iter iter;
337
338 rcu_read_lock();
339 lttng_ht_lookup(sessions_ht, &id, &iter);
340 node = lttng_ht_iter_get_node_u64(&iter);
341 if (!node) {
342 DBG("Session find by ID %" PRIu64 " id NOT found", id);
343 goto end;
344 }
345 session = caa_container_of(node, struct relay_session, session_n);
346 DBG("Session find by ID %" PRIu64 " id found", id);
347 if (!session_get(session)) {
348 session = NULL;
349 }
350 end:
351 rcu_read_unlock();
352 return session;
353 }
354
355 static 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);
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);
368 free(session);
369 }
370
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 */
376 static int session_delete(struct relay_session *session)
377 {
378 struct lttng_ht_iter iter;
379
380 iter.iter.node = &session->session_n.node;
381 return lttng_ht_del(sessions_ht, &iter);
382 }
383
384
385 static void destroy_session(struct relay_session *session)
386 {
387 int ret;
388
389 ret = session_delete(session);
390 assert(!ret);
391 lttng_trace_chunk_put(session->current_trace_chunk);
392 session->current_trace_chunk = NULL;
393 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
394 session->pending_closure_trace_chunk = NULL;
395 ret = sessiond_trace_chunk_registry_session_destroyed(
396 sessiond_trace_chunk_registry, session->sessiond_uuid);
397 assert(!ret);
398 call_rcu(&session->rcu_node, rcu_destroy_session);
399 }
400
401 void session_release(struct urcu_ref *ref)
402 {
403 struct relay_session *session =
404 caa_container_of(ref, struct relay_session, ref);
405
406 destroy_session(session);
407 }
408
409 void session_put(struct relay_session *session)
410 {
411 rcu_read_lock();
412 urcu_ref_put(&session->ref, session_release);
413 rcu_read_unlock();
414 }
415
416 int session_close(struct relay_session *session)
417 {
418 int ret = 0;
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);
426 session->connection_closed = true;
427 pthread_mutex_unlock(&session->lock);
428
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;
435 }
436 }
437 cds_list_for_each_entry_rcu(stream, &session->recv_list,
438 recv_node) {
439 /* Close streams which have not been published yet. */
440 try_stream_close(stream);
441 }
442 rcu_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;
450 }
451
452 int 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);
462 session->aborted = true;
463 pthread_mutex_unlock(&session->lock);
464 return ret;
465 }
466
467 void print_sessions(void)
468 {
469 struct lttng_ht_iter iter;
470 struct relay_session *session;
471
472 if (!sessions_ht) {
473 return;
474 }
475
476 rcu_read_lock();
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);
487 }
488 rcu_read_unlock();
489 }
This page took 0.03762 seconds and 3 git commands to generate.