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