76e48a6ab7762a11b1ead8a0fb09c854e3ceb1e5
[lttng-tools.git] / src / bin / lttng-relayd / connection.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #define _LGPL_SOURCE
21 #include <common/common.h>
22
23 #include "connection.h"
24 #include "stream.h"
25
26 static void rcu_free_connection(struct rcu_head *head)
27 {
28 struct relay_connection *conn =
29 caa_container_of(head, struct relay_connection, rcu_node);
30
31 lttcomm_destroy_sock(conn->sock);
32 connection_free(conn);
33 }
34
35 /*
36 * Must be called with a read side lock held. The read side lock must be
37 * kept until the returned relay_connection is no longer in use.
38 */
39 struct relay_connection *connection_find_by_sock(struct lttng_ht *ht, int sock)
40 {
41 struct lttng_ht_node_ulong *node;
42 struct lttng_ht_iter iter;
43 struct relay_connection *conn = NULL;
44
45 assert(ht);
46 assert(sock >= 0);
47
48 lttng_ht_lookup(ht, (void *)((unsigned long) sock), &iter);
49 node = lttng_ht_iter_get_node_ulong(&iter);
50 if (!node) {
51 DBG2("Relay connection by sock %d not found", sock);
52 goto end;
53 }
54 conn = caa_container_of(node, struct relay_connection, sock_n);
55
56 end:
57 return conn;
58 }
59
60 void connection_delete(struct lttng_ht *ht, struct relay_connection *conn)
61 {
62 int ret;
63 struct lttng_ht_iter iter;
64
65 assert(ht);
66 assert(conn);
67
68 iter.iter.node = &conn->sock_n.node;
69 ret = lttng_ht_del(ht, &iter);
70 assert(!ret);
71 }
72
73 void connection_destroy(struct relay_connection *conn)
74 {
75 assert(conn);
76
77 call_rcu(&conn->rcu_node, rcu_free_connection);
78 }
79
80 struct relay_connection *connection_create(void)
81 {
82 struct relay_connection *conn;
83
84 conn = zmalloc(sizeof(*conn));
85 if (!conn) {
86 PERROR("zmalloc relay connection");
87 goto error;
88 }
89
90 error:
91 return conn;
92 }
93
94 void connection_init(struct relay_connection *conn)
95 {
96 assert(conn);
97 assert(conn->sock);
98
99 CDS_INIT_LIST_HEAD(&conn->recv_head);
100 lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd);
101 }
102
103 void connection_free(struct relay_connection *conn)
104 {
105 assert(conn);
106
107 free(conn->viewer_session);
108 free(conn);
109 }
This page took 0.032897 seconds and 4 git commands to generate.