Commit | Line | Data |
---|---|---|
58eb9381 DG |
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 | |
6c1c0768 | 20 | #define _LGPL_SOURCE |
58eb9381 DG |
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); | |
ca741d24 | 32 | connection_free(conn); |
58eb9381 DG |
33 | } |
34 | ||
35 | struct relay_connection *connection_find_by_sock(struct lttng_ht *ht, int sock) | |
36 | { | |
37 | struct lttng_ht_node_ulong *node; | |
38 | struct lttng_ht_iter iter; | |
39 | struct relay_connection *conn = NULL; | |
40 | ||
41 | assert(ht); | |
42 | assert(sock >= 0); | |
43 | ||
44 | lttng_ht_lookup(ht, (void *)((unsigned long) sock), &iter); | |
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); | |
51 | ||
52 | end: | |
53 | return conn; | |
54 | } | |
55 | ||
56 | void connection_delete(struct lttng_ht *ht, struct relay_connection *conn) | |
57 | { | |
58 | int ret; | |
59 | struct lttng_ht_iter iter; | |
60 | ||
61 | assert(ht); | |
62 | assert(conn); | |
63 | ||
64 | iter.iter.node = &conn->sock_n.node; | |
65 | ret = lttng_ht_del(ht, &iter); | |
66 | assert(!ret); | |
67 | } | |
68 | ||
69 | void connection_destroy(struct relay_connection *conn) | |
70 | { | |
58eb9381 DG |
71 | assert(conn); |
72 | ||
58eb9381 DG |
73 | call_rcu(&conn->rcu_node, rcu_free_connection); |
74 | } | |
75 | ||
76 | struct relay_connection *connection_create(void) | |
77 | { | |
78 | struct relay_connection *conn; | |
79 | ||
80 | conn = zmalloc(sizeof(*conn)); | |
81 | if (!conn) { | |
82 | PERROR("zmalloc relay connection"); | |
83 | goto error; | |
84 | } | |
85 | ||
86 | error: | |
87 | return conn; | |
88 | } | |
89 | ||
90 | void connection_init(struct relay_connection *conn) | |
91 | { | |
92 | assert(conn); | |
93 | assert(conn->sock); | |
94 | ||
95 | CDS_INIT_LIST_HEAD(&conn->recv_head); | |
96 | lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd); | |
97 | } | |
98 | ||
99 | void connection_free(struct relay_connection *conn) | |
100 | { | |
101 | assert(conn); | |
102 | ||
ca741d24 | 103 | free(conn->viewer_session); |
58eb9381 DG |
104 | free(conn); |
105 | } |