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