| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 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 |
| 21 | #define _LGPL_SOURCE |
| 22 | #include <common/common.h> |
| 23 | #include <urcu/rculist.h> |
| 24 | |
| 25 | #include "connection.h" |
| 26 | #include "stream.h" |
| 27 | #include "viewer-session.h" |
| 28 | |
| 29 | bool connection_get(struct relay_connection *conn) |
| 30 | { |
| 31 | bool has_ref = false; |
| 32 | |
| 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; |
| 41 | } |
| 42 | |
| 43 | struct relay_connection *connection_get_by_sock(struct lttng_ht *relay_connections_ht, |
| 44 | int sock) |
| 45 | { |
| 46 | struct lttng_ht_node_ulong *node; |
| 47 | struct lttng_ht_iter iter; |
| 48 | struct relay_connection *conn = NULL; |
| 49 | |
| 50 | assert(sock >= 0); |
| 51 | |
| 52 | rcu_read_lock(); |
| 53 | lttng_ht_lookup(relay_connections_ht, (void *)((unsigned long) sock), |
| 54 | &iter); |
| 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); |
| 61 | if (!connection_get(conn)) { |
| 62 | conn = NULL; |
| 63 | } |
| 64 | end: |
| 65 | rcu_read_unlock(); |
| 66 | return conn; |
| 67 | } |
| 68 | |
| 69 | struct relay_connection *connection_create(struct lttcomm_sock *sock, |
| 70 | enum connection_type type) |
| 71 | { |
| 72 | struct relay_connection *conn; |
| 73 | |
| 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); |
| 84 | end: |
| 85 | return conn; |
| 86 | } |
| 87 | |
| 88 | static void rcu_free_connection(struct rcu_head *head) |
| 89 | { |
| 90 | struct relay_connection *conn = |
| 91 | caa_container_of(head, struct relay_connection, rcu_node); |
| 92 | |
| 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 | |
| 101 | static void destroy_connection(struct relay_connection *conn) |
| 102 | { |
| 103 | call_rcu(&conn->rcu_node, rcu_free_connection); |
| 104 | } |
| 105 | |
| 106 | static void connection_release(struct urcu_ref *ref) |
| 107 | { |
| 108 | struct relay_connection *conn = |
| 109 | caa_container_of(ref, struct relay_connection, ref); |
| 110 | |
| 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); |
| 118 | } |
| 119 | |
| 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); |
| 130 | } |
| 131 | |
| 132 | void connection_put(struct relay_connection *conn) |
| 133 | { |
| 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(); |
| 139 | } |
| 140 | |
| 141 | void connection_ht_add(struct lttng_ht *relay_connections_ht, |
| 142 | struct relay_connection *conn) |
| 143 | { |
| 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; |
| 148 | } |