2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
21 #include <common/common.h>
22 #include <common/utils.h>
23 #include <common/compat/uuid.h>
24 #include <urcu/rculist.h>
26 #include "ctf-trace.h"
27 #include "lttng-relayd.h"
29 #include "sessiond-trace-chunks.h"
31 #include <common/defaults.h>
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
;
37 static int session_set_anonymous_chunk(struct relay_session
*session
)
40 struct lttng_trace_chunk
*chunk
= NULL
;
41 enum lttng_trace_chunk_status status
;
42 struct lttng_directory_handle output_directory
;
44 bool output_path_allocated
= false;
46 if (!opt_output_path
) {
47 /* No output path defined */
48 const char *home_dir
= utils_get_home_dir();
50 ERR("Home path not found."
51 " Please specify an output path using -o, --output PATH");
55 ret
= asprintf(&output_path
, "%s/%s", home_dir
, DEFAULT_TRACE_DIR_NAME
);
57 PERROR("asprintf trace dir name");
61 output_path_allocated
= true;
63 output_path
= opt_output_path
;
64 output_path_allocated
= false;
67 ret
= lttng_directory_handle_init(&output_directory
, output_path
);
72 chunk
= lttng_trace_chunk_create_anonymous();
77 status
= lttng_trace_chunk_set_credentials_current_user(chunk
);
78 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
83 status
= lttng_trace_chunk_set_as_owner(chunk
, &output_directory
);
84 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
88 session
->current_trace_chunk
= chunk
;
91 lttng_trace_chunk_put(chunk
);
92 lttng_directory_handle_fini(&output_directory
);
93 if (output_path_allocated
) {
100 * Create a new session by assigning a new session ID.
102 * Return allocated session or else NULL.
104 struct relay_session
*session_create(const char *session_name
,
105 const char *hostname
, const char *base_path
,
108 const lttng_uuid sessiond_uuid
,
109 const uint64_t *id_sessiond
,
110 const uint64_t *current_chunk_id
,
111 const time_t *creation_time
,
114 bool session_name_contains_creation_time
)
117 struct relay_session
*session
= NULL
;
119 if (session_name
&& strstr(session_name
, ".")) {
120 ERR("Illegal character in session name: \"%s\"",
124 if (base_path
&& strstr(base_path
, "../")) {
125 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
129 if (hostname
&& strstr(hostname
, ".")) {
130 ERR("Invalid character in hostname: \"%s\"",
135 session
= zmalloc(sizeof(*session
));
137 PERROR("Failed to allocate session");
140 if (lttng_strncpy(session
->session_name
, session_name
,
141 sizeof(session
->session_name
))) {
142 WARN("Session name exceeds maximal allowed length");
145 if (lttng_strncpy(session
->hostname
, hostname
,
146 sizeof(session
->hostname
))) {
147 WARN("Hostname exceeds maximal allowed length");
150 if (lttng_strncpy(session
->base_path
, base_path
,
151 sizeof(session
->base_path
))) {
152 WARN("Base path exceeds maximal allowed length");
156 LTTNG_OPTIONAL_SET(&session
->creation_time
, *creation_time
);
158 session
->session_name_contains_creation_time
=
159 session_name_contains_creation_time
;
161 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
162 if (!session
->ctf_traces_ht
) {
166 pthread_mutex_lock(&last_relay_session_id_lock
);
167 session
->id
= ++last_relay_session_id
;
168 pthread_mutex_unlock(&last_relay_session_id_lock
);
170 session
->major
= major
;
171 session
->minor
= minor
;
172 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
173 urcu_ref_init(&session
->ref
);
174 CDS_INIT_LIST_HEAD(&session
->recv_list
);
175 pthread_mutex_init(&session
->lock
, NULL
);
176 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
178 session
->live_timer
= live_timer
;
179 session
->snapshot
= snapshot
;
180 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
183 LTTNG_OPTIONAL_SET(&session
->id_sessiond
, *id_sessiond
);
186 ret
= sessiond_trace_chunk_registry_session_created(
187 sessiond_trace_chunk_registry
, sessiond_uuid
);
192 if (id_sessiond
&& current_chunk_id
) {
193 session
->current_trace_chunk
=
194 sessiond_trace_chunk_registry_get_chunk(
195 sessiond_trace_chunk_registry
,
196 session
->sessiond_uuid
,
197 session
->id_sessiond
.value
,
199 if (!session
->current_trace_chunk
) {
200 char uuid_str
[UUID_STR_LEN
];
202 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
203 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64
", trace chunk id = %" PRIu64
,
204 uuid_str
, *id_sessiond
,
207 } else if (!id_sessiond
) {
209 * Pre-2.11 peers will not announce trace chunks. An
210 * anonymous trace chunk which will remain set for the
211 * duration of the session is created.
213 ret
= session_set_anonymous_chunk(session
);
219 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
223 session_put(session
);
227 /* Should be called with RCU read-side lock held. */
228 bool session_get(struct relay_session
*session
)
230 return urcu_ref_get_unless_zero(&session
->ref
);
234 * Lookup a session within the session hash table using the session id
235 * as key. A session reference is taken when a session is returned.
236 * session_put() must be called on that session.
238 * Return session or NULL if not found.
240 struct relay_session
*session_get_by_id(uint64_t id
)
242 struct relay_session
*session
= NULL
;
243 struct lttng_ht_node_u64
*node
;
244 struct lttng_ht_iter iter
;
247 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
248 node
= lttng_ht_iter_get_node_u64(&iter
);
250 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
253 session
= caa_container_of(node
, struct relay_session
, session_n
);
254 DBG("Session find by ID %" PRIu64
" id found", id
);
255 if (!session_get(session
)) {
263 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
265 struct relay_session
*session
=
266 caa_container_of(rcu_head
, struct relay_session
,
269 * Since each trace has a reference on the session, it means
270 * that if we are at the point where we teardown the session, no
271 * trace belonging to that session exist at this point.
272 * Calling lttng_ht_destroy in call_rcu worker thread so we
273 * don't hold the RCU read-side lock while calling it.
275 lttng_ht_destroy(session
->ctf_traces_ht
);
280 * Delete session from the given hash table.
282 * Return lttng ht del error code being 0 on success and 1 on failure.
284 static int session_delete(struct relay_session
*session
)
286 struct lttng_ht_iter iter
;
288 iter
.iter
.node
= &session
->session_n
.node
;
289 return lttng_ht_del(sessions_ht
, &iter
);
293 static void destroy_session(struct relay_session
*session
)
297 ret
= session_delete(session
);
299 lttng_trace_chunk_put(session
->current_trace_chunk
);
300 session
->current_trace_chunk
= NULL
;
301 lttng_trace_chunk_put(session
->pending_closure_trace_chunk
);
302 session
->pending_closure_trace_chunk
= NULL
;
303 ret
= sessiond_trace_chunk_registry_session_destroyed(
304 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
306 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
309 void session_release(struct urcu_ref
*ref
)
311 struct relay_session
*session
=
312 caa_container_of(ref
, struct relay_session
, ref
);
314 destroy_session(session
);
317 void session_put(struct relay_session
*session
)
320 urcu_ref_put(&session
->ref
, session_release
);
324 int session_close(struct relay_session
*session
)
327 struct ctf_trace
*trace
;
328 struct lttng_ht_iter iter
;
329 struct relay_stream
*stream
;
331 pthread_mutex_lock(&session
->lock
);
332 DBG("closing session %" PRIu64
": is conn already closed %d",
333 session
->id
, session
->connection_closed
);
334 session
->connection_closed
= true;
335 pthread_mutex_unlock(&session
->lock
);
338 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
339 &iter
.iter
, trace
, node
.node
) {
340 ret
= ctf_trace_close(trace
);
345 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
347 /* Close streams which have not been published yet. */
348 try_stream_close(stream
);
355 /* Put self-reference from create. */
356 session_put(session
);
360 int session_abort(struct relay_session
*session
)
368 pthread_mutex_lock(&session
->lock
);
369 DBG("aborting session %" PRIu64
, session
->id
);
370 session
->aborted
= true;
371 pthread_mutex_unlock(&session
->lock
);
375 void print_sessions(void)
377 struct lttng_ht_iter iter
;
378 struct relay_session
*session
;
385 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
387 if (!session_get(session
)) {
390 DBG("session %p refcount %ld session %" PRIu64
,
392 session
->ref
.refcount
,
394 session_put(session
);