Commit | Line | Data |
---|---|---|
7972aab2 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
7972aab2 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
7972aab2 | 5 | * |
7972aab2 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
c9e313bc SM |
9 | #include "buffer-registry.hpp" |
10 | #include "fd-limit.hpp" | |
c9e313bc SM |
11 | #include "lttng-ust-ctl.hpp" |
12 | #include "lttng-ust-error.hpp" | |
28ab034a | 13 | #include "ust-consumer.hpp" |
c9e313bc | 14 | #include "utils.hpp" |
7972aab2 | 15 | |
28ab034a JG |
16 | #include <common/common.hpp> |
17 | #include <common/hashtable/utils.hpp> | |
56047f5a | 18 | #include <common/urcu.hpp> |
28ab034a JG |
19 | |
20 | #include <inttypes.h> | |
21 | ||
7972aab2 DG |
22 | /* |
23 | * Set in main.c during initialization process of the daemon. This contains | |
24 | * buffer_reg_uid object which are global registry for per UID buffer. Object | |
25 | * are indexed by session id and matched by the triplet | |
26 | * <session_id/bits_per_long/uid>. | |
27 | */ | |
28 | static struct lttng_ht *buffer_registry_uid; | |
29 | ||
30 | /* | |
31 | * Initialized at the daemon start. This contains buffer_reg_pid object and | |
32 | * indexed by session id. | |
33 | */ | |
34 | static struct lttng_ht *buffer_registry_pid; | |
35 | ||
36 | /* | |
37 | * Match function for the per UID registry hash table. It matches a registry | |
38 | * uid object with the triplet <session_id/abi/uid>. | |
39 | */ | |
40 | static int ht_match_reg_uid(struct cds_lfht_node *node, const void *_key) | |
41 | { | |
42 | struct buffer_reg_uid *reg; | |
43 | const struct buffer_reg_uid *key; | |
44 | ||
a0377dfe FD |
45 | LTTNG_ASSERT(node); |
46 | LTTNG_ASSERT(_key); | |
7972aab2 DG |
47 | |
48 | reg = caa_container_of(node, struct buffer_reg_uid, node.node); | |
a0377dfe | 49 | LTTNG_ASSERT(reg); |
7966af57 | 50 | key = (buffer_reg_uid *) _key; |
7972aab2 | 51 | |
28ab034a JG |
52 | if (key->session_id != reg->session_id || key->bits_per_long != reg->bits_per_long || |
53 | key->uid != reg->uid) { | |
7972aab2 DG |
54 | goto no_match; |
55 | } | |
56 | ||
57 | /* Match */ | |
58 | return 1; | |
59 | no_match: | |
60 | return 0; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Hash function for the per UID registry hash table. This XOR the triplet | |
65 | * together. | |
66 | */ | |
bcd52dd9 | 67 | static unsigned long ht_hash_reg_uid(const void *_key, unsigned long seed) |
7972aab2 DG |
68 | { |
69 | uint64_t xored_key; | |
7966af57 | 70 | const struct buffer_reg_uid *key = (buffer_reg_uid *) _key; |
7972aab2 | 71 | |
a0377dfe | 72 | LTTNG_ASSERT(key); |
7972aab2 | 73 | |
28ab034a | 74 | xored_key = (uint64_t) (key->session_id ^ key->bits_per_long ^ key->uid); |
7972aab2 DG |
75 | return hash_key_u64(&xored_key, seed); |
76 | } | |
77 | ||
78 | /* | |
79 | * Initialize global buffer per UID registry. Should only be called ONCE!. | |
80 | */ | |
cd9adb8b | 81 | void buffer_reg_init_uid_registry() |
7972aab2 DG |
82 | { |
83 | /* Should be called once. */ | |
a0377dfe | 84 | LTTNG_ASSERT(!buffer_registry_uid); |
7972aab2 | 85 | buffer_registry_uid = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
a0377dfe | 86 | LTTNG_ASSERT(buffer_registry_uid); |
7972aab2 DG |
87 | buffer_registry_uid->match_fct = ht_match_reg_uid; |
88 | buffer_registry_uid->hash_fct = ht_hash_reg_uid; | |
89 | ||
90 | DBG3("Global buffer per UID registry initialized"); | |
91 | } | |
92 | ||
93 | /* | |
94 | * Allocate and initialize object. Set regp with the object pointer. | |
95 | * | |
96 | * Return 0 on success else a negative value and regp is untouched. | |
97 | */ | |
28ab034a JG |
98 | int buffer_reg_uid_create(uint64_t session_id, |
99 | uint32_t bits_per_long, | |
100 | uid_t uid, | |
101 | enum lttng_domain_type domain, | |
102 | struct buffer_reg_uid **regp, | |
103 | const char *root_shm_path, | |
104 | const char *shm_path) | |
7972aab2 DG |
105 | { |
106 | int ret = 0; | |
cd9adb8b | 107 | struct buffer_reg_uid *reg = nullptr; |
7972aab2 | 108 | |
a0377dfe | 109 | LTTNG_ASSERT(regp); |
7972aab2 | 110 | |
64803277 | 111 | reg = zmalloc<buffer_reg_uid>(); |
7972aab2 DG |
112 | if (!reg) { |
113 | PERROR("zmalloc buffer registry uid"); | |
114 | ret = -ENOMEM; | |
115 | goto error; | |
116 | } | |
117 | ||
64803277 | 118 | reg->registry = zmalloc<buffer_reg_session>(); |
63c861bd | 119 | if (!reg->registry) { |
7972aab2 DG |
120 | PERROR("zmalloc buffer registry uid session"); |
121 | ret = -ENOMEM; | |
122 | goto error; | |
123 | } | |
124 | ||
125 | reg->session_id = session_id; | |
126 | reg->bits_per_long = bits_per_long; | |
127 | reg->uid = uid; | |
128 | reg->domain = domain; | |
d7ba1388 | 129 | if (shm_path[0]) { |
3d071855 MD |
130 | strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path)); |
131 | reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
132 | strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path)); |
133 | reg->shm_path[sizeof(reg->shm_path) - 1] = '\0'; | |
134 | DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64, | |
28ab034a JG |
135 | reg->shm_path, |
136 | session_id); | |
d7ba1388 | 137 | } |
7972aab2 DG |
138 | reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
139 | if (!reg->registry->channels) { | |
140 | ret = -ENOMEM; | |
141 | goto error_session; | |
142 | } | |
143 | ||
144 | cds_lfht_node_init(®->node.node); | |
145 | *regp = reg; | |
146 | ||
d9bf3ca4 | 147 | DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d", |
28ab034a JG |
148 | session_id, |
149 | bits_per_long, | |
150 | uid, | |
151 | domain); | |
7972aab2 DG |
152 | |
153 | return 0; | |
154 | ||
155 | error_session: | |
156 | free(reg->registry); | |
157 | error: | |
158 | free(reg); | |
159 | return ret; | |
160 | } | |
161 | ||
162 | /* | |
163 | * Add a buffer registry per UID object to the global registry. | |
164 | */ | |
165 | void buffer_reg_uid_add(struct buffer_reg_uid *reg) | |
166 | { | |
167 | struct cds_lfht_node *nodep; | |
168 | struct lttng_ht *ht = buffer_registry_uid; | |
169 | ||
a0377dfe | 170 | LTTNG_ASSERT(reg); |
7972aab2 | 171 | |
28ab034a JG |
172 | DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64, |
173 | reg->session_id); | |
7972aab2 | 174 | |
56047f5a | 175 | lttng::urcu::read_lock_guard read_lock; |
28ab034a JG |
176 | nodep = cds_lfht_add_unique( |
177 | ht->ht, ht->hash_fct(reg, lttng_ht_seed), ht->match_fct, reg, ®->node.node); | |
a0377dfe | 178 | LTTNG_ASSERT(nodep == ®->node.node); |
7972aab2 DG |
179 | } |
180 | ||
181 | /* | |
182 | * Find a buffer registry per UID object with given params. RCU read side lock | |
183 | * MUST be acquired before calling this and hold on to protect the object. | |
184 | * | |
185 | * Return the object pointer or NULL on error. | |
186 | */ | |
28ab034a | 187 | struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id, uint32_t bits_per_long, uid_t uid) |
7972aab2 DG |
188 | { |
189 | struct lttng_ht_node_u64 *node; | |
190 | struct lttng_ht_iter iter; | |
cd9adb8b | 191 | struct buffer_reg_uid *reg = nullptr, key; |
7972aab2 DG |
192 | struct lttng_ht *ht = buffer_registry_uid; |
193 | ||
48b7cdc2 FD |
194 | ASSERT_RCU_READ_LOCKED(); |
195 | ||
7972aab2 DG |
196 | /* Setup key we are looking for. */ |
197 | key.session_id = session_id; | |
198 | key.bits_per_long = bits_per_long; | |
199 | key.uid = uid; | |
200 | ||
d9bf3ca4 | 201 | DBG3("Buffer registry per UID find id: %" PRIu64 ", ABI: %u, uid: %d", |
28ab034a JG |
202 | session_id, |
203 | bits_per_long, | |
204 | uid); | |
7972aab2 DG |
205 | |
206 | /* Custom lookup function since it's a different key. */ | |
28ab034a | 207 | cds_lfht_lookup(ht->ht, ht->hash_fct(&key, lttng_ht_seed), ht->match_fct, &key, &iter.iter); |
7972aab2 DG |
208 | node = lttng_ht_iter_get_node_u64(&iter); |
209 | if (!node) { | |
210 | goto end; | |
211 | } | |
0114db0e | 212 | reg = lttng::utils::container_of(node, &buffer_reg_uid::node); |
7972aab2 DG |
213 | |
214 | end: | |
215 | return reg; | |
216 | } | |
217 | ||
218 | /* | |
219 | * Initialize global buffer per PID registry. Should only be called ONCE!. | |
220 | */ | |
cd9adb8b | 221 | void buffer_reg_init_pid_registry() |
7972aab2 DG |
222 | { |
223 | /* Should be called once. */ | |
a0377dfe | 224 | LTTNG_ASSERT(!buffer_registry_pid); |
d9bf3ca4 | 225 | buffer_registry_pid = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
a0377dfe | 226 | LTTNG_ASSERT(buffer_registry_pid); |
7972aab2 DG |
227 | |
228 | DBG3("Global buffer per PID registry initialized"); | |
229 | } | |
230 | ||
231 | /* | |
232 | * Allocate and initialize object. Set regp with the object pointer. | |
233 | * | |
234 | * Return 0 on success else a negative value and regp is untouched. | |
235 | */ | |
28ab034a JG |
236 | int buffer_reg_pid_create(uint64_t session_id, |
237 | struct buffer_reg_pid **regp, | |
238 | const char *root_shm_path, | |
239 | const char *shm_path) | |
7972aab2 DG |
240 | { |
241 | int ret = 0; | |
cd9adb8b | 242 | struct buffer_reg_pid *reg = nullptr; |
7972aab2 | 243 | |
a0377dfe | 244 | LTTNG_ASSERT(regp); |
7972aab2 | 245 | |
64803277 | 246 | reg = zmalloc<buffer_reg_pid>(); |
7972aab2 DG |
247 | if (!reg) { |
248 | PERROR("zmalloc buffer registry pid"); | |
249 | ret = -ENOMEM; | |
250 | goto error; | |
251 | } | |
252 | ||
64803277 | 253 | reg->registry = zmalloc<buffer_reg_session>(); |
63c861bd | 254 | if (!reg->registry) { |
7972aab2 DG |
255 | PERROR("zmalloc buffer registry pid session"); |
256 | ret = -ENOMEM; | |
257 | goto error; | |
258 | } | |
259 | ||
260 | /* A cast is done here so we can use the session ID as a u64 ht node. */ | |
261 | reg->session_id = session_id; | |
d7ba1388 | 262 | if (shm_path[0]) { |
3d071855 MD |
263 | strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path)); |
264 | reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
265 | strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path)); |
266 | reg->shm_path[sizeof(reg->shm_path) - 1] = '\0'; | |
267 | DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64, | |
28ab034a JG |
268 | reg->shm_path, |
269 | session_id); | |
d7ba1388 | 270 | } |
7972aab2 DG |
271 | reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
272 | if (!reg->registry->channels) { | |
273 | ret = -ENOMEM; | |
274 | goto error_session; | |
275 | } | |
276 | ||
d9bf3ca4 | 277 | lttng_ht_node_init_u64(®->node, reg->session_id); |
7972aab2 DG |
278 | *regp = reg; |
279 | ||
28ab034a | 280 | DBG3("Buffer registry per PID created with session id: %" PRIu64, session_id); |
7972aab2 DG |
281 | |
282 | return 0; | |
283 | ||
284 | error_session: | |
285 | free(reg->registry); | |
286 | error: | |
287 | free(reg); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | /* | |
292 | * Add a buffer registry per PID object to the global registry. | |
293 | */ | |
294 | void buffer_reg_pid_add(struct buffer_reg_pid *reg) | |
295 | { | |
a0377dfe | 296 | LTTNG_ASSERT(reg); |
7972aab2 | 297 | |
d9bf3ca4 | 298 | DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64, |
28ab034a | 299 | reg->session_id); |
7972aab2 | 300 | |
56047f5a | 301 | lttng::urcu::read_lock_guard read_lock; |
d9bf3ca4 | 302 | lttng_ht_add_unique_u64(buffer_registry_pid, ®->node); |
7972aab2 DG |
303 | } |
304 | ||
305 | /* | |
306 | * Find a buffer registry per PID object with given params. RCU read side lock | |
307 | * MUST be acquired before calling this and hold on to protect the object. | |
308 | * | |
309 | * Return the object pointer or NULL on error. | |
310 | */ | |
d9bf3ca4 | 311 | struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id) |
7972aab2 | 312 | { |
d9bf3ca4 | 313 | struct lttng_ht_node_u64 *node; |
7972aab2 | 314 | struct lttng_ht_iter iter; |
cd9adb8b | 315 | struct buffer_reg_pid *reg = nullptr; |
7972aab2 DG |
316 | struct lttng_ht *ht = buffer_registry_pid; |
317 | ||
d9bf3ca4 | 318 | DBG3("Buffer registry per PID find id: %" PRIu64, session_id); |
7972aab2 | 319 | |
d9bf3ca4 MD |
320 | lttng_ht_lookup(ht, &session_id, &iter); |
321 | node = lttng_ht_iter_get_node_u64(&iter); | |
7972aab2 DG |
322 | if (!node) { |
323 | goto end; | |
324 | } | |
0114db0e | 325 | reg = lttng::utils::container_of(node, &buffer_reg_pid::node); |
7972aab2 DG |
326 | |
327 | end: | |
328 | return reg; | |
329 | } | |
330 | ||
fb83fe64 JD |
331 | /* |
332 | * Find the consumer channel key from a UST session per-uid channel key. | |
333 | * | |
334 | * Return the matching key or -1 if not found. | |
335 | */ | |
28ab034a JG |
336 | int buffer_reg_uid_consumer_channel_key(struct cds_list_head *buffer_reg_uid_list, |
337 | uint64_t chan_key, | |
338 | uint64_t *consumer_chan_key) | |
fb83fe64 JD |
339 | { |
340 | struct lttng_ht_iter iter; | |
cd9adb8b JG |
341 | struct buffer_reg_uid *uid_reg = nullptr; |
342 | struct buffer_reg_session *session_reg = nullptr; | |
fb83fe64 JD |
343 | struct buffer_reg_channel *reg_chan; |
344 | int ret = -1; | |
345 | ||
56047f5a JG |
346 | { |
347 | lttng::urcu::read_lock_guard read_lock; | |
348 | ||
349 | /* | |
350 | * For the per-uid registry, we have to iterate since we don't have the | |
351 | * uid and bitness key. | |
352 | */ | |
353 | cds_list_for_each_entry (uid_reg, buffer_reg_uid_list, lnode) { | |
354 | session_reg = uid_reg->registry; | |
355 | cds_lfht_for_each_entry ( | |
356 | session_reg->channels->ht, &iter.iter, reg_chan, node.node) { | |
357 | if (reg_chan->key == chan_key) { | |
358 | *consumer_chan_key = reg_chan->consumer_key; | |
359 | ret = 0; | |
360 | goto end; | |
361 | } | |
fb83fe64 JD |
362 | } |
363 | } | |
364 | } | |
fb83fe64 | 365 | end: |
fb83fe64 JD |
366 | return ret; |
367 | } | |
368 | ||
7972aab2 DG |
369 | /* |
370 | * Allocate and initialize a buffer registry channel with the given key. Set | |
371 | * regp with the object pointer. | |
372 | * | |
373 | * Return 0 on success or else a negative value keeping regp untouched. | |
374 | */ | |
375 | int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp) | |
376 | { | |
377 | struct buffer_reg_channel *reg; | |
378 | ||
a0377dfe | 379 | LTTNG_ASSERT(regp); |
7972aab2 DG |
380 | |
381 | DBG3("Buffer registry channel create with key: %" PRIu64, key); | |
382 | ||
64803277 | 383 | reg = zmalloc<buffer_reg_channel>(); |
7972aab2 DG |
384 | if (!reg) { |
385 | PERROR("zmalloc buffer registry channel"); | |
386 | return -ENOMEM; | |
387 | } | |
388 | ||
389 | reg->key = key; | |
390 | CDS_INIT_LIST_HEAD(®->streams); | |
cd9adb8b | 391 | pthread_mutex_init(®->stream_list_lock, nullptr); |
7972aab2 DG |
392 | |
393 | lttng_ht_node_init_u64(®->node, key); | |
394 | *regp = reg; | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
399 | /* | |
400 | * Allocate and initialize a buffer registry stream. Set regp with the object | |
401 | * pointer. | |
402 | * | |
403 | * Return 0 on success or else a negative value keeping regp untouched. | |
404 | */ | |
405 | int buffer_reg_stream_create(struct buffer_reg_stream **regp) | |
406 | { | |
407 | struct buffer_reg_stream *reg; | |
408 | ||
a0377dfe | 409 | LTTNG_ASSERT(regp); |
7972aab2 DG |
410 | |
411 | DBG3("Buffer registry creating stream"); | |
412 | ||
64803277 | 413 | reg = zmalloc<buffer_reg_stream>(); |
7972aab2 DG |
414 | if (!reg) { |
415 | PERROR("zmalloc buffer registry stream"); | |
416 | return -ENOMEM; | |
417 | } | |
418 | ||
419 | *regp = reg; | |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
424 | /* | |
425 | * Add stream to the list in the channel. | |
426 | */ | |
28ab034a | 427 | void buffer_reg_stream_add(struct buffer_reg_stream *stream, struct buffer_reg_channel *channel) |
7972aab2 | 428 | { |
a0377dfe FD |
429 | LTTNG_ASSERT(stream); |
430 | LTTNG_ASSERT(channel); | |
7972aab2 DG |
431 | |
432 | pthread_mutex_lock(&channel->stream_list_lock); | |
433 | cds_list_add_tail(&stream->lnode, &channel->streams); | |
5c786ded | 434 | channel->stream_count++; |
7972aab2 DG |
435 | pthread_mutex_unlock(&channel->stream_list_lock); |
436 | } | |
437 | ||
438 | /* | |
439 | * Add a buffer registry channel object to the given session. | |
440 | */ | |
28ab034a | 441 | void buffer_reg_channel_add(struct buffer_reg_session *session, struct buffer_reg_channel *channel) |
7972aab2 | 442 | { |
a0377dfe FD |
443 | LTTNG_ASSERT(session); |
444 | LTTNG_ASSERT(channel); | |
7972aab2 | 445 | |
56047f5a | 446 | lttng::urcu::read_lock_guard read_lock; |
7972aab2 | 447 | lttng_ht_add_unique_u64(session->channels, &channel->node); |
7972aab2 DG |
448 | } |
449 | ||
450 | /* | |
451 | * Find a buffer registry channel object with the given key. RCU read side lock | |
452 | * MUST be acquired and hold on until the object reference is not needed | |
453 | * anymore. | |
454 | * | |
455 | * Return the object pointer or NULL on error. | |
456 | */ | |
28ab034a | 457 | struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key, struct buffer_reg_uid *reg) |
7972aab2 DG |
458 | { |
459 | struct lttng_ht_node_u64 *node; | |
460 | struct lttng_ht_iter iter; | |
cd9adb8b | 461 | struct buffer_reg_channel *chan = nullptr; |
7972aab2 DG |
462 | struct lttng_ht *ht; |
463 | ||
a0377dfe | 464 | LTTNG_ASSERT(reg); |
7972aab2 DG |
465 | |
466 | switch (reg->domain) { | |
467 | case LTTNG_DOMAIN_UST: | |
468 | ht = reg->registry->channels; | |
469 | break; | |
470 | default: | |
a0377dfe | 471 | abort(); |
7972aab2 DG |
472 | goto end; |
473 | } | |
474 | ||
475 | lttng_ht_lookup(ht, &key, &iter); | |
476 | node = lttng_ht_iter_get_node_u64(&iter); | |
477 | if (!node) { | |
478 | goto end; | |
479 | } | |
0114db0e | 480 | chan = lttng::utils::container_of(node, &buffer_reg_channel::node); |
7972aab2 DG |
481 | |
482 | end: | |
483 | return chan; | |
484 | } | |
485 | ||
486 | /* | |
487 | * Destroy a buffer registry stream with the given domain. | |
488 | */ | |
28ab034a | 489 | void buffer_reg_stream_destroy(struct buffer_reg_stream *regp, enum lttng_domain_type domain) |
7972aab2 DG |
490 | { |
491 | if (!regp) { | |
492 | return; | |
493 | } | |
494 | ||
28ab034a | 495 | DBG3("Buffer registry stream destroy with handle %d", regp->obj.ust->handle); |
7972aab2 DG |
496 | |
497 | switch (domain) { | |
498 | case LTTNG_DOMAIN_UST: | |
499 | { | |
500 | int ret; | |
501 | ||
cd9adb8b | 502 | ret = ust_app_release_object(nullptr, regp->obj.ust); |
7972aab2 DG |
503 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
504 | ERR("Buffer reg stream release obj handle %d failed with ret %d", | |
28ab034a JG |
505 | regp->obj.ust->handle, |
506 | ret); | |
7972aab2 DG |
507 | } |
508 | free(regp->obj.ust); | |
509 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
510 | break; | |
511 | } | |
512 | default: | |
a0377dfe | 513 | abort(); |
7972aab2 DG |
514 | } |
515 | ||
516 | free(regp); | |
517 | return; | |
518 | } | |
519 | ||
520 | /* | |
521 | * Remove buffer registry channel object from the session hash table. RCU read | |
522 | * side lock MUST be acquired before calling this. | |
523 | */ | |
28ab034a | 524 | void buffer_reg_channel_remove(struct buffer_reg_session *session, struct buffer_reg_channel *regp) |
7972aab2 DG |
525 | { |
526 | int ret; | |
527 | struct lttng_ht_iter iter; | |
528 | ||
a0377dfe FD |
529 | LTTNG_ASSERT(session); |
530 | LTTNG_ASSERT(regp); | |
7972aab2 DG |
531 | |
532 | iter.iter.node = ®p->node.node; | |
533 | ret = lttng_ht_del(session->channels, &iter); | |
a0377dfe | 534 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
535 | } |
536 | ||
537 | /* | |
538 | * Destroy a buffer registry channel with the given domain. | |
539 | */ | |
28ab034a | 540 | void buffer_reg_channel_destroy(struct buffer_reg_channel *regp, enum lttng_domain_type domain) |
7972aab2 DG |
541 | { |
542 | if (!regp) { | |
543 | return; | |
544 | } | |
545 | ||
07d2ae95 | 546 | DBG3("Buffer registry channel destroy with key %" PRIu32, regp->key); |
7972aab2 DG |
547 | |
548 | switch (domain) { | |
549 | case LTTNG_DOMAIN_UST: | |
550 | { | |
551 | int ret; | |
552 | struct buffer_reg_stream *sreg, *stmp; | |
553 | /* Wipe stream */ | |
28ab034a | 554 | cds_list_for_each_entry_safe (sreg, stmp, ®p->streams, lnode) { |
7972aab2 | 555 | cds_list_del(&sreg->lnode); |
5c786ded | 556 | regp->stream_count--; |
7972aab2 DG |
557 | buffer_reg_stream_destroy(sreg, domain); |
558 | } | |
559 | ||
55d7e860 | 560 | if (regp->obj.ust) { |
cd9adb8b | 561 | ret = ust_app_release_object(nullptr, regp->obj.ust); |
55d7e860 MD |
562 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
563 | ERR("Buffer reg channel release obj handle %d failed with ret %d", | |
28ab034a JG |
564 | regp->obj.ust->handle, |
565 | ret); | |
55d7e860 MD |
566 | } |
567 | free(regp->obj.ust); | |
7972aab2 | 568 | } |
7972aab2 DG |
569 | lttng_fd_put(LTTNG_FD_APPS, 1); |
570 | break; | |
571 | } | |
572 | default: | |
a0377dfe | 573 | abort(); |
7972aab2 DG |
574 | } |
575 | ||
576 | free(regp); | |
577 | return; | |
578 | } | |
579 | ||
580 | /* | |
581 | * Destroy a buffer registry session with the given domain. | |
582 | */ | |
36b588ed | 583 | static void buffer_reg_session_destroy(struct buffer_reg_session *regp, |
28ab034a | 584 | enum lttng_domain_type domain) |
7972aab2 DG |
585 | { |
586 | int ret; | |
587 | struct lttng_ht_iter iter; | |
588 | struct buffer_reg_channel *reg_chan; | |
589 | ||
590 | DBG3("Buffer registry session destroy"); | |
591 | ||
592 | /* Destroy all channels. */ | |
56047f5a JG |
593 | { |
594 | lttng::urcu::read_lock_guard read_lock; | |
595 | ||
596 | cds_lfht_for_each_entry (regp->channels->ht, &iter.iter, reg_chan, node.node) { | |
597 | ret = lttng_ht_del(regp->channels, &iter); | |
598 | LTTNG_ASSERT(!ret); | |
599 | buffer_reg_channel_destroy(reg_chan, domain); | |
600 | } | |
7972aab2 | 601 | } |
7972aab2 | 602 | |
3c339053 | 603 | lttng_ht_destroy(regp->channels); |
36b588ed | 604 | |
7972aab2 DG |
605 | switch (domain) { |
606 | case LTTNG_DOMAIN_UST: | |
607 | ust_registry_session_destroy(regp->reg.ust); | |
7972aab2 DG |
608 | break; |
609 | default: | |
a0377dfe | 610 | abort(); |
7972aab2 DG |
611 | } |
612 | ||
613 | free(regp); | |
614 | return; | |
615 | } | |
616 | ||
617 | /* | |
36b588ed | 618 | * Remove buffer registry UID object from the global hash table. |
7972aab2 DG |
619 | */ |
620 | void buffer_reg_uid_remove(struct buffer_reg_uid *regp) | |
621 | { | |
622 | int ret; | |
623 | struct lttng_ht_iter iter; | |
624 | ||
a0377dfe | 625 | LTTNG_ASSERT(regp); |
7972aab2 | 626 | |
56047f5a | 627 | lttng::urcu::read_lock_guard read_lock; |
7972aab2 DG |
628 | iter.iter.node = ®p->node.node; |
629 | ret = lttng_ht_del(buffer_registry_uid, &iter); | |
a0377dfe | 630 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
631 | } |
632 | ||
633 | static void rcu_free_buffer_reg_uid(struct rcu_head *head) | |
634 | { | |
28ab034a JG |
635 | struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, <tng_ht_node_u64::head); |
636 | struct buffer_reg_uid *reg = lttng::utils::container_of(node, &buffer_reg_uid::node); | |
7972aab2 DG |
637 | |
638 | buffer_reg_session_destroy(reg->registry, reg->domain); | |
639 | free(reg); | |
640 | } | |
641 | ||
642 | static void rcu_free_buffer_reg_pid(struct rcu_head *head) | |
643 | { | |
28ab034a JG |
644 | struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, <tng_ht_node_u64::head); |
645 | struct buffer_reg_pid *reg = lttng::utils::container_of(node, &buffer_reg_pid::node); | |
7972aab2 DG |
646 | |
647 | buffer_reg_session_destroy(reg->registry, LTTNG_DOMAIN_UST); | |
648 | free(reg); | |
649 | } | |
650 | ||
651 | /* | |
652 | * Destroy buffer registry per UID. The given pointer is NOT removed from any | |
653 | * list or hash table. Use buffer_reg_pid_remove() before calling this function | |
654 | * for the case that the object is in the global hash table. | |
655 | */ | |
28ab034a | 656 | void buffer_reg_uid_destroy(struct buffer_reg_uid *regp, struct consumer_output *consumer) |
7972aab2 DG |
657 | { |
658 | struct consumer_socket *socket; | |
659 | ||
660 | if (!regp) { | |
661 | return; | |
662 | } | |
663 | ||
d9bf3ca4 | 664 | DBG3("Buffer registry per UID destroy with id: %" PRIu64 ", ABI: %u, uid: %d", |
28ab034a JG |
665 | regp->session_id, |
666 | regp->bits_per_long, | |
667 | regp->uid); | |
7972aab2 DG |
668 | |
669 | if (!consumer) { | |
670 | goto destroy; | |
671 | } | |
672 | ||
56047f5a JG |
673 | { |
674 | lttng::urcu::read_lock_guard read_lock; | |
675 | /* Get the right socket from the consumer object. */ | |
676 | socket = consumer_find_socket_by_bitness(regp->bits_per_long, consumer); | |
677 | if (!socket) { | |
678 | goto destroy; | |
679 | } | |
7972aab2 | 680 | |
56047f5a JG |
681 | switch (regp->domain) { |
682 | case LTTNG_DOMAIN_UST: | |
683 | if (regp->registry->reg.ust->_metadata_key) { | |
684 | /* Return value does not matter. This call will print errors. */ | |
685 | (void) consumer_close_metadata( | |
686 | socket, regp->registry->reg.ust->_metadata_key); | |
687 | } | |
688 | break; | |
689 | default: | |
690 | abort(); | |
691 | return; | |
7972aab2 | 692 | } |
7972aab2 DG |
693 | } |
694 | ||
695 | destroy: | |
696 | call_rcu(®p->node.head, rcu_free_buffer_reg_uid); | |
697 | } | |
698 | ||
699 | /* | |
700 | * Remove buffer registry UID object from the global hash table. RCU read side | |
701 | * lock MUST be acquired before calling this. | |
702 | */ | |
703 | void buffer_reg_pid_remove(struct buffer_reg_pid *regp) | |
704 | { | |
705 | int ret; | |
706 | struct lttng_ht_iter iter; | |
707 | ||
a0377dfe | 708 | LTTNG_ASSERT(regp); |
7972aab2 DG |
709 | |
710 | iter.iter.node = ®p->node.node; | |
711 | ret = lttng_ht_del(buffer_registry_pid, &iter); | |
a0377dfe | 712 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
713 | } |
714 | ||
715 | /* | |
716 | * Destroy buffer registry per PID. The pointer is NOT removed from the global | |
717 | * hash table. Call buffer_reg_pid_remove() before that if the object was | |
718 | * previously added to the global hash table. | |
719 | */ | |
720 | void buffer_reg_pid_destroy(struct buffer_reg_pid *regp) | |
721 | { | |
722 | if (!regp) { | |
723 | return; | |
724 | } | |
725 | ||
28ab034a | 726 | DBG3("Buffer registry per PID destroy with id: %" PRIu64, regp->session_id); |
7972aab2 DG |
727 | |
728 | /* This registry is only used by UST. */ | |
729 | call_rcu(®p->node.head, rcu_free_buffer_reg_pid); | |
730 | } | |
731 | ||
732 | /* | |
733 | * Destroy per PID and UID registry hash table. | |
734 | */ | |
cd9adb8b | 735 | void buffer_reg_destroy_registries() |
7972aab2 DG |
736 | { |
737 | DBG3("Buffer registry destroy all registry"); | |
3c339053 FD |
738 | lttng_ht_destroy(buffer_registry_uid); |
739 | lttng_ht_destroy(buffer_registry_pid); | |
7972aab2 | 740 | } |