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