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