2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
22 #include <common/common.h>
23 #include <urcu/rculist.h>
25 #include "connection.h"
27 #include "viewer-session.h"
29 bool connection_get(struct relay_connection
*conn
)
33 pthread_mutex_lock(&conn
->reflock
);
34 if (conn
->ref
.refcount
!= 0) {
36 urcu_ref_get(&conn
->ref
);
38 pthread_mutex_unlock(&conn
->reflock
);
43 struct relay_connection
*connection_get_by_sock(struct lttng_ht
*relay_connections_ht
,
46 struct lttng_ht_node_ulong
*node
;
47 struct lttng_ht_iter iter
;
48 struct relay_connection
*conn
= NULL
;
53 lttng_ht_lookup(relay_connections_ht
, (void *)((unsigned long) sock
),
55 node
= lttng_ht_iter_get_node_ulong(&iter
);
57 DBG2("Relay connection by sock %d not found", sock
);
60 conn
= caa_container_of(node
, struct relay_connection
, sock_n
);
61 if (!connection_get(conn
)) {
69 struct relay_connection
*connection_create(struct lttcomm_sock
*sock
,
70 enum connection_type type
)
72 struct relay_connection
*conn
;
74 conn
= zmalloc(sizeof(*conn
));
76 PERROR("zmalloc relay connection");
79 pthread_mutex_init(&conn
->reflock
, NULL
);
80 urcu_ref_init(&conn
->ref
);
83 lttng_ht_node_init_ulong(&conn
->sock_n
, (unsigned long) conn
->sock
->fd
);
88 static void rcu_free_connection(struct rcu_head
*head
)
90 struct relay_connection
*conn
=
91 caa_container_of(head
, struct relay_connection
, rcu_node
);
93 lttcomm_destroy_sock(conn
->sock
);
94 if (conn
->viewer_session
) {
95 viewer_session_destroy(conn
->viewer_session
);
96 conn
->viewer_session
= NULL
;
101 static void destroy_connection(struct relay_connection
*conn
)
103 call_rcu(&conn
->rcu_node
, rcu_free_connection
);
106 static void connection_release(struct urcu_ref
*ref
)
108 struct relay_connection
*conn
=
109 caa_container_of(ref
, struct relay_connection
, ref
);
111 if (conn
->in_socket_ht
) {
112 struct lttng_ht_iter iter
;
115 iter
.iter
.node
= &conn
->sock_n
.node
;
116 ret
= lttng_ht_del(conn
->socket_ht
, &iter
);
121 if (session_close(conn
->session
)) {
122 ERR("session_close");
124 conn
->session
= NULL
;
126 if (conn
->viewer_session
) {
127 viewer_session_close(conn
->viewer_session
);
129 destroy_connection(conn
);
132 void connection_put(struct relay_connection
*conn
)
135 pthread_mutex_lock(&conn
->reflock
);
136 urcu_ref_put(&conn
->ref
, connection_release
);
137 pthread_mutex_unlock(&conn
->reflock
);
141 void connection_ht_add(struct lttng_ht
*relay_connections_ht
,
142 struct relay_connection
*conn
)
144 assert(!conn
->in_socket_ht
);
145 lttng_ht_add_unique_ulong(relay_connections_ht
, &conn
->sock_n
);
146 conn
->in_socket_ht
= 1;
147 conn
->socket_ht
= relay_connections_ht
;
This page took 0.056242 seconds and 4 git commands to generate.