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