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