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> |
cc871152 | 22 | #include <common/utils.h> |
b2c25d00 | 23 | #include <common/compat/uuid.h> |
7591bab1 | 24 | #include <urcu/rculist.h> |
2a174661 | 25 | |
335875f6 JR |
26 | #include <sys/stat.h> |
27 | ||
2a174661 | 28 | #include "ctf-trace.h" |
cc871152 | 29 | #include "lttng-relayd.h" |
2f8f53af | 30 | #include "session.h" |
1f916b5d | 31 | #include "sessiond-trace-chunks.h" |
cc871152 | 32 | #include "stream.h" |
2ddab216 | 33 | #include <common/defaults.h> |
335875f6 | 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 | |
41b23598 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 | ||
b2c25d00 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 | ||
335875f6 | 135 | ret = session_init_output_directory_handle(session, &output_directory); |
b2c25d00 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, |
6ef36453 | 170 | const char *hostname, const char *base_path, |
0fb564b1 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, | |
55ce50b6 MD |
178 | uint32_t minor, |
179 | bool session_name_contains_creation_time) | |
2a174661 | 180 | { |
1f916b5d | 181 | int ret; |
99f9cbb0 JG |
182 | struct relay_session *session = NULL; |
183 | ||
7e0a8f85 JG |
184 | assert(session_name); |
185 | assert(hostname); | |
186 | assert(base_path); | |
187 | ||
188 | if (strstr(session_name, ".")) { | |
99f9cbb0 JG |
189 | ERR("Illegal character in session name: \"%s\"", |
190 | session_name); | |
191 | goto error; | |
192 | } | |
7e0a8f85 | 193 | if (strstr(base_path, "../")) { |
99f9cbb0 JG |
194 | ERR("Invalid session base path walks up the path hierarchy: \"%s\"", |
195 | base_path); | |
196 | goto error; | |
197 | } | |
7e0a8f85 | 198 | if (strstr(hostname, ".")) { |
99f9cbb0 JG |
199 | ERR("Invalid character in hostname: \"%s\"", |
200 | hostname); | |
201 | goto error; | |
202 | } | |
2a174661 DG |
203 | |
204 | session = zmalloc(sizeof(*session)); | |
205 | if (!session) { | |
b2c25d00 | 206 | PERROR("Failed to allocate session"); |
2a174661 DG |
207 | goto error; |
208 | } | |
d75e57d4 JG |
209 | |
210 | pthread_mutex_lock(&last_relay_session_id_lock); | |
211 | session->id = ++last_relay_session_id; | |
212 | pthread_mutex_unlock(&last_relay_session_id_lock); | |
213 | ||
214 | lttng_ht_node_init_u64(&session->session_n, session->id); | |
215 | urcu_ref_init(&session->ref); | |
216 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
217 | pthread_mutex_init(&session->lock, NULL); | |
218 | pthread_mutex_init(&session->recv_list_lock, NULL); | |
219 | ||
bb5d54e7 MD |
220 | if (lttng_strncpy(session->session_name, session_name, |
221 | sizeof(session->session_name))) { | |
b2c25d00 | 222 | WARN("Session name exceeds maximal allowed length"); |
bb5d54e7 MD |
223 | goto error; |
224 | } | |
225 | if (lttng_strncpy(session->hostname, hostname, | |
226 | sizeof(session->hostname))) { | |
b2c25d00 | 227 | WARN("Hostname exceeds maximal allowed length"); |
bb5d54e7 MD |
228 | goto error; |
229 | } | |
6ef36453 MD |
230 | if (lttng_strncpy(session->base_path, base_path, |
231 | sizeof(session->base_path))) { | |
232 | WARN("Base path exceeds maximal allowed length"); | |
233 | goto error; | |
234 | } | |
55ce50b6 MD |
235 | if (creation_time) { |
236 | LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); | |
237 | } | |
238 | session->session_name_contains_creation_time = | |
239 | session_name_contains_creation_time; | |
6ef36453 | 240 | |
2a174661 DG |
241 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
242 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
243 | goto error; |
244 | } | |
245 | ||
7591bab1 MD |
246 | session->major = major; |
247 | session->minor = minor; | |
7591bab1 | 248 | |
7591bab1 MD |
249 | session->live_timer = live_timer; |
250 | session->snapshot = snapshot; | |
1f916b5d JG |
251 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
252 | ||
b2c25d00 JG |
253 | if (id_sessiond) { |
254 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); | |
255 | } | |
256 | ||
9221ac46 JR |
257 | if (major == 2 && minor >= 11) { |
258 | /* Only applies for 2.11+ peers using trace chunks. */ | |
259 | ret = init_session_output_path(session); | |
260 | if (ret) { | |
261 | goto error; | |
262 | } | |
41b23598 | 263 | } |
9221ac46 | 264 | |
1f916b5d JG |
265 | ret = sessiond_trace_chunk_registry_session_created( |
266 | sessiond_trace_chunk_registry, sessiond_uuid); | |
267 | if (ret) { | |
268 | goto error; | |
269 | } | |
7591bab1 | 270 | |
b2c25d00 JG |
271 | if (id_sessiond && current_chunk_id) { |
272 | session->current_trace_chunk = | |
273 | sessiond_trace_chunk_registry_get_chunk( | |
274 | sessiond_trace_chunk_registry, | |
275 | session->sessiond_uuid, | |
276 | session->id_sessiond.value, | |
277 | *current_chunk_id); | |
278 | if (!session->current_trace_chunk) { | |
279 | char uuid_str[UUID_STR_LEN]; | |
280 | ||
281 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
282 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64, | |
283 | uuid_str, *id_sessiond, | |
284 | *current_chunk_id); | |
285 | } | |
286 | } else if (!id_sessiond) { | |
287 | /* | |
288 | * Pre-2.11 peers will not announce trace chunks. An | |
289 | * anonymous trace chunk which will remain set for the | |
290 | * duration of the session is created. | |
291 | */ | |
292 | ret = session_set_anonymous_chunk(session); | |
293 | if (ret) { | |
294 | goto error; | |
295 | } | |
296 | } | |
297 | ||
7591bab1 | 298 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
bb5d54e7 | 299 | return session; |
2a174661 DG |
300 | |
301 | error: | |
b2c25d00 | 302 | session_put(session); |
bb5d54e7 | 303 | return NULL; |
2a174661 | 304 | } |
2f8f53af | 305 | |
7591bab1 MD |
306 | /* Should be called with RCU read-side lock held. */ |
307 | bool session_get(struct relay_session *session) | |
308 | { | |
ce4d4083 | 309 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
310 | } |
311 | ||
2f8f53af | 312 | /* |
7591bab1 MD |
313 | * Lookup a session within the session hash table using the session id |
314 | * as key. A session reference is taken when a session is returned. | |
315 | * session_put() must be called on that session. | |
2f8f53af DG |
316 | * |
317 | * Return session or NULL if not found. | |
318 | */ | |
7591bab1 | 319 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
320 | { |
321 | struct relay_session *session = NULL; | |
2a174661 | 322 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
323 | struct lttng_ht_iter iter; |
324 | ||
7591bab1 MD |
325 | rcu_read_lock(); |
326 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 327 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 328 | if (!node) { |
2a174661 | 329 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
330 | goto end; |
331 | } | |
332 | session = caa_container_of(node, struct relay_session, session_n); | |
2a174661 | 333 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
334 | if (!session_get(session)) { |
335 | session = NULL; | |
336 | } | |
2f8f53af | 337 | end: |
7591bab1 | 338 | rcu_read_unlock(); |
2f8f53af DG |
339 | return session; |
340 | } | |
2a174661 | 341 | |
7591bab1 MD |
342 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
343 | { | |
344 | struct relay_session *session = | |
345 | caa_container_of(rcu_head, struct relay_session, | |
346 | rcu_node); | |
49e614cb MD |
347 | /* |
348 | * Since each trace has a reference on the session, it means | |
349 | * that if we are at the point where we teardown the session, no | |
350 | * trace belonging to that session exist at this point. | |
351 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
352 | * don't hold the RCU read-side lock while calling it. | |
353 | */ | |
354 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
355 | free(session); |
356 | } | |
357 | ||
2a174661 DG |
358 | /* |
359 | * Delete session from the given hash table. | |
360 | * | |
361 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
362 | */ | |
7591bab1 | 363 | static int session_delete(struct relay_session *session) |
2a174661 DG |
364 | { |
365 | struct lttng_ht_iter iter; | |
366 | ||
2a174661 | 367 | iter.iter.node = &session->session_n.node; |
7591bab1 | 368 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
369 | } |
370 | ||
7591bab1 MD |
371 | |
372 | static void destroy_session(struct relay_session *session) | |
373 | { | |
374 | int ret; | |
375 | ||
376 | ret = session_delete(session); | |
377 | assert(!ret); | |
86bc527a | 378 | lttng_trace_chunk_put(session->current_trace_chunk); |
84a93c08 | 379 | session->current_trace_chunk = NULL; |
a57bb1b0 JG |
380 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
381 | session->pending_closure_trace_chunk = NULL; | |
1f916b5d JG |
382 | ret = sessiond_trace_chunk_registry_session_destroyed( |
383 | sessiond_trace_chunk_registry, session->sessiond_uuid); | |
384 | assert(!ret); | |
7591bab1 MD |
385 | call_rcu(&session->rcu_node, rcu_destroy_session); |
386 | } | |
387 | ||
388 | void session_release(struct urcu_ref *ref) | |
2a174661 | 389 | { |
7591bab1 MD |
390 | struct relay_session *session = |
391 | caa_container_of(ref, struct relay_session, ref); | |
2a174661 | 392 | |
7591bab1 MD |
393 | destroy_session(session); |
394 | } | |
2a174661 | 395 | |
7591bab1 MD |
396 | void session_put(struct relay_session *session) |
397 | { | |
2b847234 JG |
398 | if (!session) { |
399 | return; | |
400 | } | |
7591bab1 | 401 | rcu_read_lock(); |
7591bab1 | 402 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 403 | rcu_read_unlock(); |
2a174661 DG |
404 | } |
405 | ||
7591bab1 | 406 | int session_close(struct relay_session *session) |
2a174661 DG |
407 | { |
408 | int ret = 0; | |
7591bab1 MD |
409 | struct ctf_trace *trace; |
410 | struct lttng_ht_iter iter; | |
411 | struct relay_stream *stream; | |
412 | ||
413 | pthread_mutex_lock(&session->lock); | |
414 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
415 | session->id, session->connection_closed); | |
7591bab1 | 416 | session->connection_closed = true; |
7591bab1 | 417 | pthread_mutex_unlock(&session->lock); |
2a174661 | 418 | |
7591bab1 MD |
419 | rcu_read_lock(); |
420 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, | |
421 | &iter.iter, trace, node.node) { | |
422 | ret = ctf_trace_close(trace); | |
423 | if (ret) { | |
424 | goto rcu_unlock; | |
2a174661 DG |
425 | } |
426 | } | |
7591bab1 MD |
427 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
428 | recv_node) { | |
bda7c7b9 JG |
429 | /* Close streams which have not been published yet. */ |
430 | try_stream_close(stream); | |
7591bab1 MD |
431 | } |
432 | rcu_unlock: | |
433 | rcu_read_unlock(); | |
434 | if (ret) { | |
435 | return ret; | |
436 | } | |
437 | /* Put self-reference from create. */ | |
438 | session_put(session); | |
439 | return ret; | |
2a174661 DG |
440 | } |
441 | ||
98ba050e JR |
442 | int session_abort(struct relay_session *session) |
443 | { | |
444 | int ret = 0; | |
445 | ||
446 | if (!session) { | |
447 | return 0; | |
448 | } | |
449 | ||
450 | pthread_mutex_lock(&session->lock); | |
451 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 452 | session->aborted = true; |
98ba050e JR |
453 | pthread_mutex_unlock(&session->lock); |
454 | return ret; | |
455 | } | |
456 | ||
7591bab1 | 457 | void print_sessions(void) |
2a174661 | 458 | { |
2a174661 | 459 | struct lttng_ht_iter iter; |
7591bab1 | 460 | struct relay_session *session; |
2a174661 | 461 | |
ce3f3ba3 JG |
462 | if (!sessions_ht) { |
463 | return; | |
464 | } | |
465 | ||
2a174661 | 466 | rcu_read_lock(); |
7591bab1 MD |
467 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
468 | session_n.node) { | |
469 | if (!session_get(session)) { | |
470 | continue; | |
471 | } | |
472 | DBG("session %p refcount %ld session %" PRIu64, | |
473 | session, | |
474 | session->ref.refcount, | |
475 | session->id); | |
476 | session_put(session); | |
2a174661 | 477 | } |
2a174661 | 478 | rcu_read_unlock(); |
2a174661 | 479 | } |
335875f6 JR |
480 | |
481 | int session_init_output_directory_handle(struct relay_session *session, | |
482 | struct lttng_directory_handle *handle) | |
483 | { | |
484 | int ret; | |
485 | /* | |
486 | * relayd_output_path/session_directory | |
487 | * e.g. /home/user/lttng-traces/hostname/session_name | |
488 | */ | |
489 | char *full_session_path = NULL; | |
490 | ||
491 | pthread_mutex_lock(&session->lock); | |
492 | full_session_path = create_output_path(session->output_path); | |
493 | if (!full_session_path) { | |
494 | ret = -1; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | ret = utils_mkdir_recursive( | |
499 | full_session_path, S_IRWXU | S_IRWXG, -1, -1); | |
500 | if (ret) { | |
501 | ERR("Failed to create session output path \"%s\"", | |
502 | full_session_path); | |
503 | goto end; | |
504 | } | |
505 | ||
506 | ret = lttng_directory_handle_init(handle, full_session_path); | |
507 | if (ret) { | |
508 | goto end; | |
509 | } | |
510 | end: | |
511 | pthread_mutex_unlock(&session->lock); | |
512 | free(full_session_path); | |
513 | return ret; | |
514 | } |