| 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 | #include <common/common.h> |
| 21 | |
| 22 | #include "connection.h" |
| 23 | #include "stream.h" |
| 24 | |
| 25 | static void rcu_free_connection(struct rcu_head *head) |
| 26 | { |
| 27 | struct relay_connection *conn = |
| 28 | caa_container_of(head, struct relay_connection, rcu_node); |
| 29 | |
| 30 | lttcomm_destroy_sock(conn->sock); |
| 31 | connection_free(conn); |
| 32 | } |
| 33 | |
| 34 | struct relay_connection *connection_find_by_sock(struct lttng_ht *ht, int sock) |
| 35 | { |
| 36 | struct lttng_ht_node_ulong *node; |
| 37 | struct lttng_ht_iter iter; |
| 38 | struct relay_connection *conn = NULL; |
| 39 | |
| 40 | assert(ht); |
| 41 | assert(sock >= 0); |
| 42 | |
| 43 | lttng_ht_lookup(ht, (void *)((unsigned long) sock), &iter); |
| 44 | node = lttng_ht_iter_get_node_ulong(&iter); |
| 45 | if (!node) { |
| 46 | DBG2("Relay connection by sock %d not found", sock); |
| 47 | goto end; |
| 48 | } |
| 49 | conn = caa_container_of(node, struct relay_connection, sock_n); |
| 50 | |
| 51 | end: |
| 52 | return conn; |
| 53 | } |
| 54 | |
| 55 | void connection_delete(struct lttng_ht *ht, struct relay_connection *conn) |
| 56 | { |
| 57 | int ret; |
| 58 | struct lttng_ht_iter iter; |
| 59 | |
| 60 | assert(ht); |
| 61 | assert(conn); |
| 62 | |
| 63 | iter.iter.node = &conn->sock_n.node; |
| 64 | ret = lttng_ht_del(ht, &iter); |
| 65 | assert(!ret); |
| 66 | } |
| 67 | |
| 68 | void connection_destroy(struct relay_connection *conn) |
| 69 | { |
| 70 | struct relay_stream *stream, *tmp_stream; |
| 71 | |
| 72 | assert(conn); |
| 73 | |
| 74 | /* Clean up recv list of this connection if any. */ |
| 75 | cds_list_for_each_entry_safe(stream, tmp_stream, &conn->recv_head, |
| 76 | recv_list) { |
| 77 | cds_list_del(&stream->recv_list); |
| 78 | } |
| 79 | |
| 80 | call_rcu(&conn->rcu_node, rcu_free_connection); |
| 81 | } |
| 82 | |
| 83 | struct relay_connection *connection_create(void) |
| 84 | { |
| 85 | struct relay_connection *conn; |
| 86 | |
| 87 | conn = zmalloc(sizeof(*conn)); |
| 88 | if (!conn) { |
| 89 | PERROR("zmalloc relay connection"); |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | error: |
| 94 | return conn; |
| 95 | } |
| 96 | |
| 97 | void connection_init(struct relay_connection *conn) |
| 98 | { |
| 99 | assert(conn); |
| 100 | assert(conn->sock); |
| 101 | |
| 102 | CDS_INIT_LIST_HEAD(&conn->recv_head); |
| 103 | lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd); |
| 104 | } |
| 105 | |
| 106 | void connection_free(struct relay_connection *conn) |
| 107 | { |
| 108 | assert(conn); |
| 109 | |
| 110 | free(conn->viewer_session); |
| 111 | free(conn); |
| 112 | } |