Commit | Line | Data |
---|---|---|
58eb9381 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> |
58eb9381 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 |
58eb9381 | 21 | #include <common/common.h> |
7591bab1 | 22 | #include <urcu/rculist.h> |
58eb9381 DG |
23 | |
24 | #include "connection.h" | |
25 | #include "stream.h" | |
7591bab1 | 26 | #include "viewer-session.h" |
58eb9381 | 27 | |
7591bab1 | 28 | bool connection_get(struct relay_connection *conn) |
58eb9381 | 29 | { |
ce4d4083 | 30 | return urcu_ref_get_unless_zero(&conn->ref); |
58eb9381 DG |
31 | } |
32 | ||
7591bab1 MD |
33 | struct relay_connection *connection_get_by_sock(struct lttng_ht *relay_connections_ht, |
34 | int sock) | |
58eb9381 DG |
35 | { |
36 | struct lttng_ht_node_ulong *node; | |
37 | struct lttng_ht_iter iter; | |
38 | struct relay_connection *conn = NULL; | |
39 | ||
58eb9381 DG |
40 | assert(sock >= 0); |
41 | ||
7591bab1 MD |
42 | rcu_read_lock(); |
43 | lttng_ht_lookup(relay_connections_ht, (void *)((unsigned long) sock), | |
44 | &iter); | |
58eb9381 DG |
45 | node = lttng_ht_iter_get_node_ulong(&iter); |
46 | if (!node) { | |
47 | DBG2("Relay connection by sock %d not found", sock); | |
48 | goto end; | |
49 | } | |
50 | conn = caa_container_of(node, struct relay_connection, sock_n); | |
7591bab1 MD |
51 | if (!connection_get(conn)) { |
52 | conn = NULL; | |
53 | } | |
58eb9381 | 54 | end: |
7591bab1 | 55 | rcu_read_unlock(); |
58eb9381 DG |
56 | return conn; |
57 | } | |
58 | ||
7591bab1 MD |
59 | struct relay_connection *connection_create(struct lttcomm_sock *sock, |
60 | enum connection_type type) | |
58eb9381 | 61 | { |
7591bab1 | 62 | struct relay_connection *conn; |
58eb9381 | 63 | |
7591bab1 MD |
64 | conn = zmalloc(sizeof(*conn)); |
65 | if (!conn) { | |
66 | PERROR("zmalloc relay connection"); | |
67 | goto end; | |
68 | } | |
7591bab1 MD |
69 | urcu_ref_init(&conn->ref); |
70 | conn->type = type; | |
71 | conn->sock = sock; | |
72 | lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd); | |
73 | end: | |
74 | return conn; | |
58eb9381 DG |
75 | } |
76 | ||
7591bab1 | 77 | static void rcu_free_connection(struct rcu_head *head) |
58eb9381 | 78 | { |
7591bab1 MD |
79 | struct relay_connection *conn = |
80 | caa_container_of(head, struct relay_connection, rcu_node); | |
58eb9381 | 81 | |
7591bab1 MD |
82 | lttcomm_destroy_sock(conn->sock); |
83 | if (conn->viewer_session) { | |
84 | viewer_session_destroy(conn->viewer_session); | |
85 | conn->viewer_session = NULL; | |
86 | } | |
87 | free(conn); | |
88 | } | |
89 | ||
90 | static void destroy_connection(struct relay_connection *conn) | |
91 | { | |
58eb9381 DG |
92 | call_rcu(&conn->rcu_node, rcu_free_connection); |
93 | } | |
94 | ||
7591bab1 | 95 | static void connection_release(struct urcu_ref *ref) |
58eb9381 | 96 | { |
7591bab1 MD |
97 | struct relay_connection *conn = |
98 | caa_container_of(ref, struct relay_connection, ref); | |
58eb9381 | 99 | |
7591bab1 MD |
100 | if (conn->in_socket_ht) { |
101 | struct lttng_ht_iter iter; | |
102 | int ret; | |
103 | ||
104 | iter.iter.node = &conn->sock_n.node; | |
105 | ret = lttng_ht_del(conn->socket_ht, &iter); | |
106 | assert(!ret); | |
58eb9381 DG |
107 | } |
108 | ||
7591bab1 MD |
109 | if (conn->session) { |
110 | if (session_close(conn->session)) { | |
111 | ERR("session_close"); | |
112 | } | |
113 | conn->session = NULL; | |
114 | } | |
115 | if (conn->viewer_session) { | |
116 | viewer_session_close(conn->viewer_session); | |
117 | } | |
118 | destroy_connection(conn); | |
58eb9381 DG |
119 | } |
120 | ||
7591bab1 | 121 | void connection_put(struct relay_connection *conn) |
58eb9381 | 122 | { |
7591bab1 | 123 | rcu_read_lock(); |
7591bab1 | 124 | urcu_ref_put(&conn->ref, connection_release); |
7591bab1 | 125 | rcu_read_unlock(); |
58eb9381 DG |
126 | } |
127 | ||
7591bab1 MD |
128 | void connection_ht_add(struct lttng_ht *relay_connections_ht, |
129 | struct relay_connection *conn) | |
58eb9381 | 130 | { |
7591bab1 MD |
131 | assert(!conn->in_socket_ht); |
132 | lttng_ht_add_unique_ulong(relay_connections_ht, &conn->sock_n); | |
133 | conn->in_socket_ht = 1; | |
134 | conn->socket_ht = relay_connections_ht; | |
58eb9381 | 135 | } |