de059aae203e2df17054f9bd1d415279e0bd23bf
[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
32 /* Global session id used in the session creation. */
33 static uint64_t last_relay_session_id;
34 static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
35
36 static int session_set_anonymous_chunk(struct relay_session *session)
37 {
38 int ret = 0;
39 struct lttng_trace_chunk *chunk = NULL;
40 enum lttng_trace_chunk_status status;
41 struct lttng_directory_handle output_directory;
42 const char *base_path = opt_output_path;
43
44 if (base_path == NULL) {
45 /* No output path defined */
46 base_path = utils_get_home_dir();
47 if (base_path == NULL) {
48 ERR("Home path not found.\n \
49 Please specify an output path using -o, --output PATH");
50 ret = -1;
51 goto end;
52 }
53 }
54
55 ret = lttng_directory_handle_init(&output_directory, base_path);
56 if (ret) {
57 goto end;
58 }
59
60 chunk = lttng_trace_chunk_create_anonymous();
61 if (!chunk) {
62 goto end;
63 }
64
65 status = lttng_trace_chunk_set_credentials_current_user(chunk);
66 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
67 ret = -1;
68 goto end;
69 }
70
71 status = lttng_trace_chunk_set_as_owner(chunk, &output_directory);
72 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
73 ret = -1;
74 goto end;
75 }
76 session->current_trace_chunk = chunk;
77 chunk = NULL;
78 end:
79 lttng_trace_chunk_put(chunk);
80 lttng_directory_handle_fini(&output_directory);
81 return ret;
82 }
83
84 /*
85 * Create a new session by assigning a new session ID.
86 *
87 * Return allocated session or else NULL.
88 */
89 struct relay_session *session_create(const char *session_name,
90 const char *hostname, const char *base_path,
91 uint32_t live_timer,
92 bool snapshot,
93 const lttng_uuid sessiond_uuid,
94 const uint64_t *id_sessiond,
95 const uint64_t *current_chunk_id,
96 const time_t *creation_time,
97 uint32_t major,
98 uint32_t minor)
99 {
100 int ret;
101 struct relay_session *session = NULL;
102
103 if (session_name && strstr(session_name, ".")) {
104 ERR("Illegal character in session name: \"%s\"",
105 session_name);
106 goto error;
107 }
108 if (base_path && strstr(base_path, "../")) {
109 ERR("Invalid session base path walks up the path hierarchy: \"%s\"",
110 base_path);
111 goto error;
112 }
113 if (hostname && strstr(hostname, ".")) {
114 ERR("Invalid character in hostname: \"%s\"",
115 hostname);
116 goto error;
117 }
118
119 session = zmalloc(sizeof(*session));
120 if (!session) {
121 PERROR("Failed to allocate session");
122 goto error;
123 }
124 if (lttng_strncpy(session->session_name, session_name,
125 sizeof(session->session_name))) {
126 WARN("Session name exceeds maximal allowed length");
127 goto error;
128 }
129 if (lttng_strncpy(session->hostname, hostname,
130 sizeof(session->hostname))) {
131 WARN("Hostname exceeds maximal allowed length");
132 goto error;
133 }
134 if (lttng_strncpy(session->base_path, base_path,
135 sizeof(session->base_path))) {
136 WARN("Base path exceeds maximal allowed length");
137 goto error;
138 }
139
140 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
141 if (!session->ctf_traces_ht) {
142 goto error;
143 }
144
145 pthread_mutex_lock(&last_relay_session_id_lock);
146 session->id = ++last_relay_session_id;
147 pthread_mutex_unlock(&last_relay_session_id_lock);
148
149 session->major = major;
150 session->minor = minor;
151 lttng_ht_node_init_u64(&session->session_n, session->id);
152 urcu_ref_init(&session->ref);
153 CDS_INIT_LIST_HEAD(&session->recv_list);
154 pthread_mutex_init(&session->lock, NULL);
155 pthread_mutex_init(&session->recv_list_lock, NULL);
156
157 session->live_timer = live_timer;
158 session->snapshot = snapshot;
159 lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid);
160
161 if (id_sessiond) {
162 LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond);
163 }
164
165 ret = sessiond_trace_chunk_registry_session_created(
166 sessiond_trace_chunk_registry, sessiond_uuid);
167 if (ret) {
168 goto error;
169 }
170
171 if (id_sessiond && current_chunk_id) {
172 session->current_trace_chunk =
173 sessiond_trace_chunk_registry_get_chunk(
174 sessiond_trace_chunk_registry,
175 session->sessiond_uuid,
176 session->id_sessiond.value,
177 *current_chunk_id);
178 if (!session->current_trace_chunk) {
179 char uuid_str[UUID_STR_LEN];
180
181 lttng_uuid_to_str(sessiond_uuid, uuid_str);
182 ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64,
183 uuid_str, *id_sessiond,
184 *current_chunk_id);
185 }
186 } else if (!id_sessiond) {
187 /*
188 * Pre-2.11 peers will not announce trace chunks. An
189 * anonymous trace chunk which will remain set for the
190 * duration of the session is created.
191 */
192 ret = session_set_anonymous_chunk(session);
193 if (ret) {
194 goto error;
195 }
196 }
197
198 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
199 if (creation_time) {
200 LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time);
201 }
202 return session;
203
204 error:
205 session_put(session);
206 return NULL;
207 }
208
209 /* Should be called with RCU read-side lock held. */
210 bool session_get(struct relay_session *session)
211 {
212 return urcu_ref_get_unless_zero(&session->ref);
213 }
214
215 /*
216 * Lookup a session within the session hash table using the session id
217 * as key. A session reference is taken when a session is returned.
218 * session_put() must be called on that session.
219 *
220 * Return session or NULL if not found.
221 */
222 struct relay_session *session_get_by_id(uint64_t id)
223 {
224 struct relay_session *session = NULL;
225 struct lttng_ht_node_u64 *node;
226 struct lttng_ht_iter iter;
227
228 rcu_read_lock();
229 lttng_ht_lookup(sessions_ht, &id, &iter);
230 node = lttng_ht_iter_get_node_u64(&iter);
231 if (!node) {
232 DBG("Session find by ID %" PRIu64 " id NOT found", id);
233 goto end;
234 }
235 session = caa_container_of(node, struct relay_session, session_n);
236 DBG("Session find by ID %" PRIu64 " id found", id);
237 if (!session_get(session)) {
238 session = NULL;
239 }
240 end:
241 rcu_read_unlock();
242 return session;
243 }
244
245 static void rcu_destroy_session(struct rcu_head *rcu_head)
246 {
247 struct relay_session *session =
248 caa_container_of(rcu_head, struct relay_session,
249 rcu_node);
250 /*
251 * Since each trace has a reference on the session, it means
252 * that if we are at the point where we teardown the session, no
253 * trace belonging to that session exist at this point.
254 * Calling lttng_ht_destroy in call_rcu worker thread so we
255 * don't hold the RCU read-side lock while calling it.
256 */
257 lttng_ht_destroy(session->ctf_traces_ht);
258 free(session);
259 }
260
261 /*
262 * Delete session from the given hash table.
263 *
264 * Return lttng ht del error code being 0 on success and 1 on failure.
265 */
266 static int session_delete(struct relay_session *session)
267 {
268 struct lttng_ht_iter iter;
269
270 iter.iter.node = &session->session_n.node;
271 return lttng_ht_del(sessions_ht, &iter);
272 }
273
274
275 static void destroy_session(struct relay_session *session)
276 {
277 int ret;
278
279 ret = session_delete(session);
280 assert(!ret);
281 lttng_trace_chunk_put(session->current_trace_chunk);
282 session->current_trace_chunk = NULL;
283 lttng_trace_chunk_put(session->pending_closure_trace_chunk);
284 session->pending_closure_trace_chunk = NULL;
285 ret = sessiond_trace_chunk_registry_session_destroyed(
286 sessiond_trace_chunk_registry, session->sessiond_uuid);
287 assert(!ret);
288 call_rcu(&session->rcu_node, rcu_destroy_session);
289 }
290
291 void session_release(struct urcu_ref *ref)
292 {
293 struct relay_session *session =
294 caa_container_of(ref, struct relay_session, ref);
295
296 destroy_session(session);
297 }
298
299 void session_put(struct relay_session *session)
300 {
301 rcu_read_lock();
302 urcu_ref_put(&session->ref, session_release);
303 rcu_read_unlock();
304 }
305
306 int session_close(struct relay_session *session)
307 {
308 int ret = 0;
309 struct ctf_trace *trace;
310 struct lttng_ht_iter iter;
311 struct relay_stream *stream;
312
313 pthread_mutex_lock(&session->lock);
314 DBG("closing session %" PRIu64 ": is conn already closed %d",
315 session->id, session->connection_closed);
316 session->connection_closed = true;
317 pthread_mutex_unlock(&session->lock);
318
319 rcu_read_lock();
320 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
321 &iter.iter, trace, node.node) {
322 ret = ctf_trace_close(trace);
323 if (ret) {
324 goto rcu_unlock;
325 }
326 }
327 cds_list_for_each_entry_rcu(stream, &session->recv_list,
328 recv_node) {
329 /* Close streams which have not been published yet. */
330 try_stream_close(stream);
331 }
332 rcu_unlock:
333 rcu_read_unlock();
334 if (ret) {
335 return ret;
336 }
337 /* Put self-reference from create. */
338 session_put(session);
339 return ret;
340 }
341
342 int session_abort(struct relay_session *session)
343 {
344 int ret = 0;
345
346 if (!session) {
347 return 0;
348 }
349
350 pthread_mutex_lock(&session->lock);
351 DBG("aborting session %" PRIu64, session->id);
352 session->aborted = true;
353 pthread_mutex_unlock(&session->lock);
354 return ret;
355 }
356
357 void print_sessions(void)
358 {
359 struct lttng_ht_iter iter;
360 struct relay_session *session;
361
362 if (!sessions_ht) {
363 return;
364 }
365
366 rcu_read_lock();
367 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
368 session_n.node) {
369 if (!session_get(session)) {
370 continue;
371 }
372 DBG("session %p refcount %ld session %" PRIu64,
373 session,
374 session->ref.refcount,
375 session->id);
376 session_put(session);
377 }
378 rcu_read_unlock();
379 }
This page took 0.035309 seconds and 3 git commands to generate.