Fix: illegal memory access in session_create
[lttng-tools.git] / src / bin / lttng-relayd / session.c
CommitLineData
2f8f53af
DG
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5a693d30 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
2a174661 20#define _GNU_SOURCE
6c1c0768 21#define _LGPL_SOURCE
2a174661 22#include <common/common.h>
5a693d30 23#include <urcu/rculist.h>
2a174661 24
5a693d30 25#include "lttng-relayd.h"
2a174661 26#include "ctf-trace.h"
2f8f53af 27#include "session.h"
2a174661
DG
28#include "stream.h"
29
30/* Global session id used in the session creation. */
31static uint64_t last_relay_session_id;
5a693d30 32static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
2a174661
DG
33
34/*
35 * Create a new session by assigning a new session ID.
36 *
37 * Return allocated session or else NULL.
38 */
5a693d30
MD
39struct relay_session *session_create(const char *session_name,
40 const char *hostname, uint32_t live_timer,
41 bool snapshot, uint32_t major, uint32_t minor)
2a174661
DG
42{
43 struct relay_session *session;
44
45 session = zmalloc(sizeof(*session));
46 if (!session) {
47 PERROR("relay session zmalloc");
48 goto error;
49 }
2e8d52df
MD
50 if (lttng_strncpy(session->session_name, session_name,
51 sizeof(session->session_name))) {
52 goto error;
53 }
54 if (lttng_strncpy(session->hostname, hostname,
55 sizeof(session->hostname))) {
56 goto error;
57 }
2a174661
DG
58 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
59 if (!session->ctf_traces_ht) {
2a174661
DG
60 goto error;
61 }
62
5a693d30 63 pthread_mutex_lock(&last_relay_session_id_lock);
2a174661 64 session->id = ++last_relay_session_id;
5a693d30
MD
65 pthread_mutex_unlock(&last_relay_session_id_lock);
66
67 session->major = major;
68 session->minor = minor;
2a174661 69 lttng_ht_node_init_u64(&session->session_n, session->id);
5a693d30
MD
70 urcu_ref_init(&session->ref);
71 CDS_INIT_LIST_HEAD(&session->recv_list);
72 pthread_mutex_init(&session->lock, NULL);
73 pthread_mutex_init(&session->reflock, NULL);
74 pthread_mutex_init(&session->recv_list_lock, NULL);
75
5a693d30
MD
76 session->live_timer = live_timer;
77 session->snapshot = snapshot;
78
79 lttng_ht_add_unique_u64(sessions_ht, &session->session_n);
2e8d52df 80 return session;
2a174661
DG
81
82error:
2e8d52df
MD
83 free(session);
84 return NULL;
2a174661 85}
2f8f53af 86
5a693d30
MD
87/* Should be called with RCU read-side lock held. */
88bool session_get(struct relay_session *session)
89{
90 bool has_ref = false;
91
92 pthread_mutex_lock(&session->reflock);
93 if (session->ref.refcount != 0) {
94 has_ref = true;
95 urcu_ref_get(&session->ref);
96 }
97 pthread_mutex_unlock(&session->reflock);
98
99 return has_ref;
100}
101
2f8f53af 102/*
5a693d30
MD
103 * Lookup a session within the session hash table using the session id
104 * as key. A session reference is taken when a session is returned.
105 * session_put() must be called on that session.
2f8f53af
DG
106 *
107 * Return session or NULL if not found.
108 */
5a693d30 109struct relay_session *session_get_by_id(uint64_t id)
2f8f53af
DG
110{
111 struct relay_session *session = NULL;
2a174661 112 struct lttng_ht_node_u64 *node;
2f8f53af
DG
113 struct lttng_ht_iter iter;
114
5a693d30
MD
115 rcu_read_lock();
116 lttng_ht_lookup(sessions_ht, &id, &iter);
2a174661 117 node = lttng_ht_iter_get_node_u64(&iter);
2f8f53af 118 if (!node) {
2a174661 119 DBG("Session find by ID %" PRIu64 " id NOT found", id);
2f8f53af
DG
120 goto end;
121 }
122 session = caa_container_of(node, struct relay_session, session_n);
2a174661 123 DBG("Session find by ID %" PRIu64 " id found", id);
5a693d30
MD
124 if (!session_get(session)) {
125 session = NULL;
126 }
2f8f53af 127end:
5a693d30 128 rcu_read_unlock();
2f8f53af
DG
129 return session;
130}
2a174661 131
5a693d30
MD
132static void rcu_destroy_session(struct rcu_head *rcu_head)
133{
134 struct relay_session *session =
135 caa_container_of(rcu_head, struct relay_session,
136 rcu_node);
c65ce455
MD
137 /*
138 * Since each trace has a reference on the session, it means
139 * that if we are at the point where we teardown the session, no
140 * trace belonging to that session exist at this point.
141 * Calling lttng_ht_destroy in call_rcu worker thread so we
142 * don't hold the RCU read-side lock while calling it.
143 */
144 lttng_ht_destroy(session->ctf_traces_ht);
5a693d30
MD
145 free(session);
146}
147
2a174661
DG
148/*
149 * Delete session from the given hash table.
150 *
151 * Return lttng ht del error code being 0 on success and 1 on failure.
152 */
5a693d30 153static int session_delete(struct relay_session *session)
2a174661
DG
154{
155 struct lttng_ht_iter iter;
156
2a174661 157 iter.iter.node = &session->session_n.node;
5a693d30 158 return lttng_ht_del(sessions_ht, &iter);
2a174661
DG
159}
160
5a693d30
MD
161
162static void destroy_session(struct relay_session *session)
163{
164 int ret;
165
166 ret = session_delete(session);
167 assert(!ret);
5a693d30
MD
168 call_rcu(&session->rcu_node, rcu_destroy_session);
169}
170
171void session_release(struct urcu_ref *ref)
2a174661 172{
5a693d30
MD
173 struct relay_session *session =
174 caa_container_of(ref, struct relay_session, ref);
2a174661 175
5a693d30
MD
176 destroy_session(session);
177}
2a174661 178
5a693d30
MD
179void session_put(struct relay_session *session)
180{
181 rcu_read_lock();
182 pthread_mutex_lock(&session->reflock);
183 urcu_ref_put(&session->ref, session_release);
184 pthread_mutex_unlock(&session->reflock);
185 rcu_read_unlock();
2a174661
DG
186}
187
5a693d30 188int session_close(struct relay_session *session)
2a174661
DG
189{
190 int ret = 0;
5a693d30
MD
191 struct ctf_trace *trace;
192 struct lttng_ht_iter iter;
193 struct relay_stream *stream;
194
195 pthread_mutex_lock(&session->lock);
196 DBG("closing session %" PRIu64 ": is conn already closed %d",
197 session->id, session->connection_closed);
198 if (session->connection_closed) {
199 ret = -1;
200 goto unlock;
201 }
202 session->connection_closed = true;
203unlock:
204 pthread_mutex_unlock(&session->lock);
205 if (ret) {
206 return ret;
207 }
2a174661 208
5a693d30
MD
209 rcu_read_lock();
210 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
211 &iter.iter, trace, node.node) {
212 ret = ctf_trace_close(trace);
213 if (ret) {
214 goto rcu_unlock;
2a174661
DG
215 }
216 }
5a693d30
MD
217 cds_list_for_each_entry_rcu(stream, &session->recv_list,
218 recv_node) {
a08107cc
JG
219 /* Close streams which have not been published yet. */
220 try_stream_close(stream);
5a693d30
MD
221 }
222rcu_unlock:
223 rcu_read_unlock();
224 if (ret) {
225 return ret;
226 }
227 /* Put self-reference from create. */
228 session_put(session);
229 return ret;
2a174661
DG
230}
231
5a693d30 232void print_sessions(void)
2a174661 233{
2a174661 234 struct lttng_ht_iter iter;
5a693d30 235 struct relay_session *session;
2a174661 236
d71e55a5
JG
237 if (!sessions_ht) {
238 return;
239 }
240
2a174661 241 rcu_read_lock();
5a693d30
MD
242 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
243 session_n.node) {
244 if (!session_get(session)) {
245 continue;
246 }
247 DBG("session %p refcount %ld session %" PRIu64,
248 session,
249 session->ref.refcount,
250 session->id);
251 session_put(session);
2a174661 252 }
2a174661 253 rcu_read_unlock();
2a174661 254}
This page took 0.03868 seconds and 4 git commands to generate.