Fix: Memory leak in relay_add_stream error path
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
CommitLineData
d3e2ba59
JD
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 <assert.h>
21
22#include <common/common.h>
23#include <common/utils.h>
24
25#include "ctf-trace.h"
2a174661
DG
26#include "lttng-relayd.h"
27#include "stream.h"
d3e2ba59
JD
28
29static uint64_t last_relay_ctf_trace_id;
30
2a174661
DG
31static void rcu_destroy_ctf_trace(struct rcu_head *head)
32{
33 struct lttng_ht_node_str *node =
34 caa_container_of(head, struct lttng_ht_node_str, head);
35 struct ctf_trace *trace=
36 caa_container_of(node, struct ctf_trace, node);
37
38 free(trace);
39}
40
198d75ca
JG
41static void rcu_destroy_stream(struct rcu_head *head)
42{
43 struct relay_stream *stream =
44 caa_container_of(head, struct relay_stream, rcu_node);
45
46 stream_destroy(stream);
47}
48
d3e2ba59 49/*
2a174661
DG
50 * Destroy a ctf trace and all stream contained in it.
51 *
52 * MUST be called with the RCU read side lock.
d3e2ba59 53 */
2a174661 54void ctf_trace_destroy(struct ctf_trace *obj)
d3e2ba59 55{
2a174661
DG
56 struct relay_stream *stream, *tmp_stream;
57
58 assert(obj);
59 /*
60 * Getting to this point, every stream referenced to that object have put
61 * back their ref since the've been closed by the control side.
62 */
63 assert(!obj->refcount);
64
65 cds_list_for_each_entry_safe(stream, tmp_stream, &obj->stream_list,
66 trace_list) {
67 stream_delete(relay_streams_ht, stream);
198d75ca 68 call_rcu(&stream->rcu_node, rcu_destroy_stream);
d3e2ba59
JD
69 }
70
2a174661
DG
71 call_rcu(&obj->node.head, rcu_destroy_ctf_trace);
72}
73
74void ctf_trace_try_destroy(struct relay_session *session,
75 struct ctf_trace *ctf_trace)
76{
77 assert(session);
78 assert(ctf_trace);
79
80 /*
81 * Considering no viewer attach to the session and the trace having no more
82 * stream attached, wipe the trace.
83 */
84 if (uatomic_read(&session->viewer_refcount) == 0 &&
85 uatomic_read(&ctf_trace->refcount) == 0) {
c46bab29 86 ctf_trace_delete(session->ctf_traces_ht, ctf_trace);
2a174661 87 ctf_trace_destroy(ctf_trace);
d3e2ba59
JD
88 }
89}
90
91/*
92 * Create and return an allocated ctf_trace object. NULL on error.
93 */
2a174661 94struct ctf_trace *ctf_trace_create(char *path_name)
d3e2ba59
JD
95{
96 struct ctf_trace *obj;
97
2a174661
DG
98 assert(path_name);
99
d3e2ba59
JD
100 obj = zmalloc(sizeof(*obj));
101 if (!obj) {
102 PERROR("ctf_trace alloc");
103 goto error;
104 }
105
2a174661
DG
106 CDS_INIT_LIST_HEAD(&obj->stream_list);
107
d3e2ba59 108 obj->id = ++last_relay_ctf_trace_id;
2a174661
DG
109 lttng_ht_node_init_str(&obj->node, path_name);
110
111 DBG("Created ctf_trace %" PRIu64 " with path: %s", obj->id, path_name);
d3e2ba59
JD
112
113error:
114 return obj;
115}
116
117/*
2a174661 118 * Return a ctf_trace object if found by id in the given hash table else NULL.
d3e2ba59 119 */
2a174661
DG
120struct ctf_trace *ctf_trace_find_by_path(struct lttng_ht *ht,
121 char *path_name)
d3e2ba59 122{
2a174661 123 struct lttng_ht_node_str *node;
d3e2ba59 124 struct lttng_ht_iter iter;
2a174661 125 struct ctf_trace *trace = NULL;
d3e2ba59
JD
126
127 assert(ht);
2a174661
DG
128
129 lttng_ht_lookup(ht, (void *) path_name, &iter);
130 node = lttng_ht_iter_get_node_str(&iter);
131 if (!node) {
132 DBG("CTF Trace path %s not found", path_name);
133 goto end;
d3e2ba59 134 }
2a174661 135 trace = caa_container_of(node, struct ctf_trace, node);
d3e2ba59
JD
136
137end:
2a174661 138 return trace;
d3e2ba59
JD
139}
140
2a174661
DG
141/*
142 * Add stream to a given hash table.
143 */
144void ctf_trace_add(struct lttng_ht *ht, struct ctf_trace *trace)
145{
146 assert(ht);
147 assert(trace);
148
149 lttng_ht_add_str(ht, &trace->node);
150}
151
152/*
153 * Delete stream from a given hash table.
154 */
155void ctf_trace_delete(struct lttng_ht *ht, struct ctf_trace *trace)
156{
157 int ret;
158 struct lttng_ht_iter iter;
159
160 assert(ht);
161 assert(trace);
162
163 iter.iter.node = &trace->node.node;
164 ret = lttng_ht_del(ht, &iter);
165 assert(!ret);
166}
This page took 0.032054 seconds and 4 git commands to generate.