Fix: app contexts: do not leak app context name, event field, context field
[lttng-ust.git] / liblttng-ust / lttng-context-provider.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST application context provider.
7 */
8
9#define _LGPL_SOURCE
10#include <stddef.h>
11#include <stdint.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <ust-context-provider.h>
16
17#include "context-internal.h"
18#include "lttng-tracer-core.h"
19#include "jhash.h"
20#include "context-provider-internal.h"
21#include <ust-helper.h>
22
23struct lttng_ust_registered_context_provider {
24 const struct lttng_ust_context_provider *provider;
25
26 struct cds_hlist_node node;
27};
28
29struct lttng_ust_app_ctx {
30 char *name;
31 struct lttng_ust_event_field *event_field;
32 struct lttng_ust_type_common *type;
33};
34
35#define CONTEXT_PROVIDER_HT_BITS 12
36#define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
37struct context_provider_ht {
38 struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE];
39};
40
41static struct context_provider_ht context_provider_ht;
42
43static const struct lttng_ust_context_provider *
44 lookup_provider_by_name(const char *name)
45{
46 struct cds_hlist_head *head;
47 struct cds_hlist_node *node;
48 struct lttng_ust_registered_context_provider *reg_provider;
49 uint32_t hash;
50 const char *end;
51 size_t len;
52
53 /* Lookup using everything before first ':' as key. */
54 end = strchr(name, ':');
55 if (end)
56 len = end - name;
57 else
58 len = strlen(name);
59 hash = jhash(name, len, 0);
60 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
61 cds_hlist_for_each_entry(reg_provider, node, head, node) {
62 if (!strncmp(reg_provider->provider->name, name, len))
63 return reg_provider->provider;
64 }
65 return NULL;
66}
67
68struct lttng_ust_registered_context_provider *lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
69{
70 struct lttng_ust_registered_context_provider *reg_provider = NULL;
71 struct cds_hlist_head *head;
72 size_t name_len = strlen(provider->name);
73 uint32_t hash;
74
75 lttng_ust_fixup_tls();
76
77 /* Provider name starts with "$app.". */
78 if (strncmp("$app.", provider->name, strlen("$app.")) != 0)
79 return NULL;
80 /* Provider name cannot contain a colon character. */
81 if (strchr(provider->name, ':'))
82 return NULL;
83 if (ust_lock())
84 goto end;
85 if (lookup_provider_by_name(provider->name))
86 goto end;
87 reg_provider = zmalloc(sizeof(struct lttng_ust_registered_context_provider));
88 if (!reg_provider)
89 goto end;
90 reg_provider->provider = provider;
91 hash = jhash(provider->name, name_len, 0);
92 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
93 cds_hlist_add_head(&reg_provider->node, head);
94
95 lttng_ust_context_set_session_provider(provider->name,
96 provider->get_size, provider->record,
97 provider->get_value, provider->priv);
98
99 lttng_ust_context_set_event_notifier_group_provider(provider->name,
100 provider->get_size, provider->record,
101 provider->get_value, provider->priv);
102end:
103 ust_unlock();
104 return reg_provider;
105}
106
107void lttng_ust_context_provider_unregister(struct lttng_ust_registered_context_provider *reg_provider)
108{
109 lttng_ust_fixup_tls();
110
111 if (ust_lock())
112 goto end;
113 lttng_ust_context_set_session_provider(reg_provider->provider->name,
114 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
115 lttng_ust_dummy_get_value, NULL);
116
117 lttng_ust_context_set_event_notifier_group_provider(reg_provider->provider->name,
118 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
119 lttng_ust_dummy_get_value, NULL);
120
121 cds_hlist_del(&reg_provider->node);
122end:
123 ust_unlock();
124 free(reg_provider);
125}
126
127static void destroy_app_ctx(void *priv)
128{
129 struct lttng_ust_app_ctx *app_ctx = (struct lttng_ust_app_ctx *) priv;
130
131 free(app_ctx->name);
132 free(app_ctx->event_field);
133 free(app_ctx->type);
134 free(app_ctx);
135}
136
137/*
138 * Called with ust mutex held.
139 * Add application context to array of context, even if the application
140 * context is not currently loaded by application. It will then use the
141 * dummy callbacks in that case.
142 * Always performed before tracing is started, since it modifies
143 * metadata describing the context.
144 */
145int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
146 struct lttng_ust_ctx **ctx)
147{
148 const struct lttng_ust_context_provider *provider;
149 struct lttng_ust_ctx_field new_field = { 0 };
150 struct lttng_ust_event_field *event_field = NULL;
151 struct lttng_ust_type_common *type = NULL;
152 struct lttng_ust_app_ctx *app_ctx = NULL;
153 char *ctx_name;
154 int ret;
155
156 if (*ctx && lttng_find_context(*ctx, name))
157 return -EEXIST;
158 event_field = zmalloc(sizeof(struct lttng_ust_event_field));
159 if (!event_field) {
160 ret = -ENOMEM;
161 goto error_event_field_alloc;
162 }
163 ctx_name = strdup(name);
164 if (!ctx_name) {
165 ret = -ENOMEM;
166 goto error_field_name_alloc;
167 }
168 type = zmalloc(sizeof(struct lttng_ust_type_common));
169 if (!type) {
170 ret = -ENOMEM;
171 goto error_field_type_alloc;
172 }
173 app_ctx = zmalloc(sizeof(struct lttng_ust_app_ctx));
174 if (!app_ctx) {
175 ret = -ENOMEM;
176 goto error_app_ctx_alloc;
177 }
178 event_field->name = ctx_name;
179 type->type = lttng_ust_type_dynamic;
180 event_field->type = type;
181 new_field.event_field = event_field;
182 /*
183 * If provider is not found, we add the context anyway, but
184 * it will provide a dummy context.
185 */
186 provider = lookup_provider_by_name(name);
187 if (provider) {
188 new_field.get_size = provider->get_size;
189 new_field.record = provider->record;
190 new_field.get_value = provider->get_value;
191 } else {
192 new_field.get_size = lttng_ust_dummy_get_size;
193 new_field.record = lttng_ust_dummy_record;
194 new_field.get_value = lttng_ust_dummy_get_value;
195 }
196 new_field.destroy = destroy_app_ctx;
197 new_field.priv = app_ctx;
198 /*
199 * For application context, add it by expanding
200 * ctx array.
201 */
202 ret = lttng_ust_context_append_rcu(ctx, &new_field);
203 if (ret) {
204 destroy_app_ctx(app_ctx);
205 return ret;
206 }
207 return 0;
208
209error_app_ctx_alloc:
210 free(type);
211error_field_type_alloc:
212 free(ctx_name);
213error_field_name_alloc:
214 free(event_field);
215error_event_field_alloc:
216 return ret;
217}
This page took 0.026718 seconds and 4 git commands to generate.