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