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> | |
18 | ||
19 | #include <inttypes.h> | |
20 | ||
7972aab2 DG |
21 | /* |
22 | * Set in main.c during initialization process of the daemon. This contains | |
23 | * buffer_reg_uid object which are global registry for per UID buffer. Object | |
24 | * are indexed by session id and matched by the triplet | |
25 | * <session_id/bits_per_long/uid>. | |
26 | */ | |
27 | static struct lttng_ht *buffer_registry_uid; | |
28 | ||
29 | /* | |
30 | * Initialized at the daemon start. This contains buffer_reg_pid object and | |
31 | * indexed by session id. | |
32 | */ | |
33 | static struct lttng_ht *buffer_registry_pid; | |
34 | ||
35 | /* | |
36 | * Match function for the per UID registry hash table. It matches a registry | |
37 | * uid object with the triplet <session_id/abi/uid>. | |
38 | */ | |
39 | static int ht_match_reg_uid(struct cds_lfht_node *node, const void *_key) | |
40 | { | |
41 | struct buffer_reg_uid *reg; | |
42 | const struct buffer_reg_uid *key; | |
43 | ||
a0377dfe FD |
44 | LTTNG_ASSERT(node); |
45 | LTTNG_ASSERT(_key); | |
7972aab2 DG |
46 | |
47 | reg = caa_container_of(node, struct buffer_reg_uid, node.node); | |
a0377dfe | 48 | LTTNG_ASSERT(reg); |
7966af57 | 49 | key = (buffer_reg_uid *) _key; |
7972aab2 | 50 | |
28ab034a JG |
51 | if (key->session_id != reg->session_id || key->bits_per_long != reg->bits_per_long || |
52 | key->uid != reg->uid) { | |
7972aab2 DG |
53 | goto no_match; |
54 | } | |
55 | ||
56 | /* Match */ | |
57 | return 1; | |
58 | no_match: | |
59 | return 0; | |
60 | } | |
61 | ||
62 | /* | |
63 | * Hash function for the per UID registry hash table. This XOR the triplet | |
64 | * together. | |
65 | */ | |
bcd52dd9 | 66 | static unsigned long ht_hash_reg_uid(const void *_key, unsigned long seed) |
7972aab2 DG |
67 | { |
68 | uint64_t xored_key; | |
7966af57 | 69 | const struct buffer_reg_uid *key = (buffer_reg_uid *) _key; |
7972aab2 | 70 | |
a0377dfe | 71 | LTTNG_ASSERT(key); |
7972aab2 | 72 | |
28ab034a | 73 | xored_key = (uint64_t) (key->session_id ^ key->bits_per_long ^ key->uid); |
7972aab2 DG |
74 | return hash_key_u64(&xored_key, seed); |
75 | } | |
76 | ||
77 | /* | |
78 | * Initialize global buffer per UID registry. Should only be called ONCE!. | |
79 | */ | |
80 | void buffer_reg_init_uid_registry(void) | |
81 | { | |
82 | /* Should be called once. */ | |
a0377dfe | 83 | LTTNG_ASSERT(!buffer_registry_uid); |
7972aab2 | 84 | buffer_registry_uid = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
a0377dfe | 85 | LTTNG_ASSERT(buffer_registry_uid); |
7972aab2 DG |
86 | buffer_registry_uid->match_fct = ht_match_reg_uid; |
87 | buffer_registry_uid->hash_fct = ht_hash_reg_uid; | |
88 | ||
89 | DBG3("Global buffer per UID registry initialized"); | |
90 | } | |
91 | ||
92 | /* | |
93 | * Allocate and initialize object. Set regp with the object pointer. | |
94 | * | |
95 | * Return 0 on success else a negative value and regp is untouched. | |
96 | */ | |
28ab034a JG |
97 | int buffer_reg_uid_create(uint64_t session_id, |
98 | uint32_t bits_per_long, | |
99 | uid_t uid, | |
100 | enum lttng_domain_type domain, | |
101 | struct buffer_reg_uid **regp, | |
102 | const char *root_shm_path, | |
103 | const char *shm_path) | |
7972aab2 DG |
104 | { |
105 | int ret = 0; | |
106 | struct buffer_reg_uid *reg = NULL; | |
107 | ||
a0377dfe | 108 | LTTNG_ASSERT(regp); |
7972aab2 | 109 | |
64803277 | 110 | reg = zmalloc<buffer_reg_uid>(); |
7972aab2 DG |
111 | if (!reg) { |
112 | PERROR("zmalloc buffer registry uid"); | |
113 | ret = -ENOMEM; | |
114 | goto error; | |
115 | } | |
116 | ||
64803277 | 117 | reg->registry = zmalloc<buffer_reg_session>(); |
63c861bd | 118 | if (!reg->registry) { |
7972aab2 DG |
119 | PERROR("zmalloc buffer registry uid session"); |
120 | ret = -ENOMEM; | |
121 | goto error; | |
122 | } | |
123 | ||
124 | reg->session_id = session_id; | |
125 | reg->bits_per_long = bits_per_long; | |
126 | reg->uid = uid; | |
127 | reg->domain = domain; | |
d7ba1388 | 128 | if (shm_path[0]) { |
3d071855 MD |
129 | strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path)); |
130 | reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
131 | strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path)); |
132 | reg->shm_path[sizeof(reg->shm_path) - 1] = '\0'; | |
133 | DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64, | |
28ab034a JG |
134 | reg->shm_path, |
135 | session_id); | |
d7ba1388 | 136 | } |
7972aab2 DG |
137 | reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
138 | if (!reg->registry->channels) { | |
139 | ret = -ENOMEM; | |
140 | goto error_session; | |
141 | } | |
142 | ||
143 | cds_lfht_node_init(®->node.node); | |
144 | *regp = reg; | |
145 | ||
d9bf3ca4 | 146 | DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d", |
28ab034a JG |
147 | session_id, |
148 | bits_per_long, | |
149 | uid, | |
150 | domain); | |
7972aab2 DG |
151 | |
152 | return 0; | |
153 | ||
154 | error_session: | |
155 | free(reg->registry); | |
156 | error: | |
157 | free(reg); | |
158 | return ret; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Add a buffer registry per UID object to the global registry. | |
163 | */ | |
164 | void buffer_reg_uid_add(struct buffer_reg_uid *reg) | |
165 | { | |
166 | struct cds_lfht_node *nodep; | |
167 | struct lttng_ht *ht = buffer_registry_uid; | |
168 | ||
a0377dfe | 169 | LTTNG_ASSERT(reg); |
7972aab2 | 170 | |
28ab034a JG |
171 | DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64, |
172 | reg->session_id); | |
7972aab2 DG |
173 | |
174 | rcu_read_lock(); | |
28ab034a JG |
175 | nodep = cds_lfht_add_unique( |
176 | ht->ht, ht->hash_fct(reg, lttng_ht_seed), ht->match_fct, reg, ®->node.node); | |
a0377dfe | 177 | LTTNG_ASSERT(nodep == ®->node.node); |
7972aab2 DG |
178 | rcu_read_unlock(); |
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; | |
191 | struct buffer_reg_uid *reg = NULL, key; | |
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 | */ | |
221 | void buffer_reg_init_pid_registry(void) | |
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; | |
242 | struct buffer_reg_pid *reg = NULL; | |
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 DG |
300 | |
301 | rcu_read_lock(); | |
d9bf3ca4 | 302 | lttng_ht_add_unique_u64(buffer_registry_pid, ®->node); |
7972aab2 DG |
303 | rcu_read_unlock(); |
304 | } | |
305 | ||
306 | /* | |
307 | * Find a buffer registry per PID object with given params. RCU read side lock | |
308 | * MUST be acquired before calling this and hold on to protect the object. | |
309 | * | |
310 | * Return the object pointer or NULL on error. | |
311 | */ | |
d9bf3ca4 | 312 | struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id) |
7972aab2 | 313 | { |
d9bf3ca4 | 314 | struct lttng_ht_node_u64 *node; |
7972aab2 DG |
315 | struct lttng_ht_iter iter; |
316 | struct buffer_reg_pid *reg = NULL; | |
317 | struct lttng_ht *ht = buffer_registry_pid; | |
318 | ||
d9bf3ca4 | 319 | DBG3("Buffer registry per PID find id: %" PRIu64, session_id); |
7972aab2 | 320 | |
d9bf3ca4 MD |
321 | lttng_ht_lookup(ht, &session_id, &iter); |
322 | node = lttng_ht_iter_get_node_u64(&iter); | |
7972aab2 DG |
323 | if (!node) { |
324 | goto end; | |
325 | } | |
0114db0e | 326 | reg = lttng::utils::container_of(node, &buffer_reg_pid::node); |
7972aab2 DG |
327 | |
328 | end: | |
329 | return reg; | |
330 | } | |
331 | ||
fb83fe64 JD |
332 | /* |
333 | * Find the consumer channel key from a UST session per-uid channel key. | |
334 | * | |
335 | * Return the matching key or -1 if not found. | |
336 | */ | |
28ab034a JG |
337 | int buffer_reg_uid_consumer_channel_key(struct cds_list_head *buffer_reg_uid_list, |
338 | uint64_t chan_key, | |
339 | uint64_t *consumer_chan_key) | |
fb83fe64 JD |
340 | { |
341 | struct lttng_ht_iter iter; | |
342 | struct buffer_reg_uid *uid_reg = NULL; | |
343 | struct buffer_reg_session *session_reg = NULL; | |
344 | struct buffer_reg_channel *reg_chan; | |
345 | int ret = -1; | |
346 | ||
347 | rcu_read_lock(); | |
348 | /* | |
349 | * For the per-uid registry, we have to iterate since we don't have the | |
350 | * uid and bitness key. | |
351 | */ | |
28ab034a | 352 | cds_list_for_each_entry (uid_reg, buffer_reg_uid_list, lnode) { |
fb83fe64 | 353 | session_reg = uid_reg->registry; |
28ab034a JG |
354 | cds_lfht_for_each_entry ( |
355 | session_reg->channels->ht, &iter.iter, reg_chan, node.node) { | |
fb83fe64 JD |
356 | if (reg_chan->key == chan_key) { |
357 | *consumer_chan_key = reg_chan->consumer_key; | |
358 | ret = 0; | |
359 | goto end; | |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | end: | |
365 | rcu_read_unlock(); | |
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); | |
391 | pthread_mutex_init(®->stream_list_lock, NULL); | |
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 DG |
445 | |
446 | rcu_read_lock(); | |
447 | lttng_ht_add_unique_u64(session->channels, &channel->node); | |
448 | rcu_read_unlock(); | |
449 | } | |
450 | ||
451 | /* | |
452 | * Find a buffer registry channel object with the given key. RCU read side lock | |
453 | * MUST be acquired and hold on until the object reference is not needed | |
454 | * anymore. | |
455 | * | |
456 | * Return the object pointer or NULL on error. | |
457 | */ | |
28ab034a | 458 | struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key, struct buffer_reg_uid *reg) |
7972aab2 DG |
459 | { |
460 | struct lttng_ht_node_u64 *node; | |
461 | struct lttng_ht_iter iter; | |
462 | struct buffer_reg_channel *chan = NULL; | |
463 | struct lttng_ht *ht; | |
464 | ||
a0377dfe | 465 | LTTNG_ASSERT(reg); |
7972aab2 DG |
466 | |
467 | switch (reg->domain) { | |
468 | case LTTNG_DOMAIN_UST: | |
469 | ht = reg->registry->channels; | |
470 | break; | |
471 | default: | |
a0377dfe | 472 | abort(); |
7972aab2 DG |
473 | goto end; |
474 | } | |
475 | ||
476 | lttng_ht_lookup(ht, &key, &iter); | |
477 | node = lttng_ht_iter_get_node_u64(&iter); | |
478 | if (!node) { | |
479 | goto end; | |
480 | } | |
0114db0e | 481 | chan = lttng::utils::container_of(node, &buffer_reg_channel::node); |
7972aab2 DG |
482 | |
483 | end: | |
484 | return chan; | |
485 | } | |
486 | ||
487 | /* | |
488 | * Destroy a buffer registry stream with the given domain. | |
489 | */ | |
28ab034a | 490 | void buffer_reg_stream_destroy(struct buffer_reg_stream *regp, enum lttng_domain_type domain) |
7972aab2 DG |
491 | { |
492 | if (!regp) { | |
493 | return; | |
494 | } | |
495 | ||
28ab034a | 496 | DBG3("Buffer registry stream destroy with handle %d", regp->obj.ust->handle); |
7972aab2 DG |
497 | |
498 | switch (domain) { | |
499 | case LTTNG_DOMAIN_UST: | |
500 | { | |
501 | int ret; | |
502 | ||
fb45065e | 503 | ret = ust_app_release_object(NULL, regp->obj.ust); |
7972aab2 DG |
504 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
505 | ERR("Buffer reg stream release obj handle %d failed with ret %d", | |
28ab034a JG |
506 | regp->obj.ust->handle, |
507 | ret); | |
7972aab2 DG |
508 | } |
509 | free(regp->obj.ust); | |
510 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
511 | break; | |
512 | } | |
513 | default: | |
a0377dfe | 514 | abort(); |
7972aab2 DG |
515 | } |
516 | ||
517 | free(regp); | |
518 | return; | |
519 | } | |
520 | ||
521 | /* | |
522 | * Remove buffer registry channel object from the session hash table. RCU read | |
523 | * side lock MUST be acquired before calling this. | |
524 | */ | |
28ab034a | 525 | void buffer_reg_channel_remove(struct buffer_reg_session *session, struct buffer_reg_channel *regp) |
7972aab2 DG |
526 | { |
527 | int ret; | |
528 | struct lttng_ht_iter iter; | |
529 | ||
a0377dfe FD |
530 | LTTNG_ASSERT(session); |
531 | LTTNG_ASSERT(regp); | |
7972aab2 DG |
532 | |
533 | iter.iter.node = ®p->node.node; | |
534 | ret = lttng_ht_del(session->channels, &iter); | |
a0377dfe | 535 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
536 | } |
537 | ||
538 | /* | |
539 | * Destroy a buffer registry channel with the given domain. | |
540 | */ | |
28ab034a | 541 | void buffer_reg_channel_destroy(struct buffer_reg_channel *regp, enum lttng_domain_type domain) |
7972aab2 DG |
542 | { |
543 | if (!regp) { | |
544 | return; | |
545 | } | |
546 | ||
07d2ae95 | 547 | DBG3("Buffer registry channel destroy with key %" PRIu32, regp->key); |
7972aab2 DG |
548 | |
549 | switch (domain) { | |
550 | case LTTNG_DOMAIN_UST: | |
551 | { | |
552 | int ret; | |
553 | struct buffer_reg_stream *sreg, *stmp; | |
554 | /* Wipe stream */ | |
28ab034a | 555 | cds_list_for_each_entry_safe (sreg, stmp, ®p->streams, lnode) { |
7972aab2 | 556 | cds_list_del(&sreg->lnode); |
5c786ded | 557 | regp->stream_count--; |
7972aab2 DG |
558 | buffer_reg_stream_destroy(sreg, domain); |
559 | } | |
560 | ||
55d7e860 | 561 | if (regp->obj.ust) { |
fb45065e | 562 | ret = ust_app_release_object(NULL, regp->obj.ust); |
55d7e860 MD |
563 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
564 | ERR("Buffer reg channel release obj handle %d failed with ret %d", | |
28ab034a JG |
565 | regp->obj.ust->handle, |
566 | ret); | |
55d7e860 MD |
567 | } |
568 | free(regp->obj.ust); | |
7972aab2 | 569 | } |
7972aab2 DG |
570 | lttng_fd_put(LTTNG_FD_APPS, 1); |
571 | break; | |
572 | } | |
573 | default: | |
a0377dfe | 574 | abort(); |
7972aab2 DG |
575 | } |
576 | ||
577 | free(regp); | |
578 | return; | |
579 | } | |
580 | ||
581 | /* | |
582 | * Destroy a buffer registry session with the given domain. | |
583 | */ | |
36b588ed | 584 | static void buffer_reg_session_destroy(struct buffer_reg_session *regp, |
28ab034a | 585 | enum lttng_domain_type domain) |
7972aab2 DG |
586 | { |
587 | int ret; | |
588 | struct lttng_ht_iter iter; | |
589 | struct buffer_reg_channel *reg_chan; | |
590 | ||
591 | DBG3("Buffer registry session destroy"); | |
592 | ||
593 | /* Destroy all channels. */ | |
594 | rcu_read_lock(); | |
28ab034a | 595 | cds_lfht_for_each_entry (regp->channels->ht, &iter.iter, reg_chan, node.node) { |
7972aab2 | 596 | ret = lttng_ht_del(regp->channels, &iter); |
a0377dfe | 597 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
598 | buffer_reg_channel_destroy(reg_chan, domain); |
599 | } | |
7972aab2 DG |
600 | rcu_read_unlock(); |
601 | ||
3c339053 | 602 | lttng_ht_destroy(regp->channels); |
36b588ed | 603 | |
7972aab2 DG |
604 | switch (domain) { |
605 | case LTTNG_DOMAIN_UST: | |
606 | ust_registry_session_destroy(regp->reg.ust); | |
7972aab2 DG |
607 | break; |
608 | default: | |
a0377dfe | 609 | abort(); |
7972aab2 DG |
610 | } |
611 | ||
612 | free(regp); | |
613 | return; | |
614 | } | |
615 | ||
616 | /* | |
36b588ed | 617 | * Remove buffer registry UID object from the global hash table. |
7972aab2 DG |
618 | */ |
619 | void buffer_reg_uid_remove(struct buffer_reg_uid *regp) | |
620 | { | |
621 | int ret; | |
622 | struct lttng_ht_iter iter; | |
623 | ||
a0377dfe | 624 | LTTNG_ASSERT(regp); |
7972aab2 | 625 | |
36b588ed | 626 | rcu_read_lock(); |
7972aab2 DG |
627 | iter.iter.node = ®p->node.node; |
628 | ret = lttng_ht_del(buffer_registry_uid, &iter); | |
a0377dfe | 629 | LTTNG_ASSERT(!ret); |
36b588ed | 630 | rcu_read_unlock(); |
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 | ||
36b588ed | 673 | rcu_read_lock(); |
7972aab2 | 674 | /* Get the right socket from the consumer object. */ |
28ab034a | 675 | socket = consumer_find_socket_by_bitness(regp->bits_per_long, consumer); |
7972aab2 | 676 | if (!socket) { |
36b588ed | 677 | goto unlock; |
7972aab2 DG |
678 | } |
679 | ||
680 | switch (regp->domain) { | |
681 | case LTTNG_DOMAIN_UST: | |
aeeb48c6 | 682 | if (regp->registry->reg.ust->_metadata_key) { |
7972aab2 DG |
683 | /* Return value does not matter. This call will print errors. */ |
684 | (void) consumer_close_metadata(socket, | |
28ab034a | 685 | regp->registry->reg.ust->_metadata_key); |
7972aab2 DG |
686 | } |
687 | break; | |
688 | default: | |
a0377dfe | 689 | abort(); |
36b588ed | 690 | rcu_read_unlock(); |
7972aab2 DG |
691 | return; |
692 | } | |
693 | ||
36b588ed MD |
694 | unlock: |
695 | rcu_read_unlock(); | |
7972aab2 DG |
696 | destroy: |
697 | call_rcu(®p->node.head, rcu_free_buffer_reg_uid); | |
698 | } | |
699 | ||
700 | /* | |
701 | * Remove buffer registry UID object from the global hash table. RCU read side | |
702 | * lock MUST be acquired before calling this. | |
703 | */ | |
704 | void buffer_reg_pid_remove(struct buffer_reg_pid *regp) | |
705 | { | |
706 | int ret; | |
707 | struct lttng_ht_iter iter; | |
708 | ||
a0377dfe | 709 | LTTNG_ASSERT(regp); |
7972aab2 DG |
710 | |
711 | iter.iter.node = ®p->node.node; | |
712 | ret = lttng_ht_del(buffer_registry_pid, &iter); | |
a0377dfe | 713 | LTTNG_ASSERT(!ret); |
7972aab2 DG |
714 | } |
715 | ||
716 | /* | |
717 | * Destroy buffer registry per PID. The pointer is NOT removed from the global | |
718 | * hash table. Call buffer_reg_pid_remove() before that if the object was | |
719 | * previously added to the global hash table. | |
720 | */ | |
721 | void buffer_reg_pid_destroy(struct buffer_reg_pid *regp) | |
722 | { | |
723 | if (!regp) { | |
724 | return; | |
725 | } | |
726 | ||
28ab034a | 727 | DBG3("Buffer registry per PID destroy with id: %" PRIu64, regp->session_id); |
7972aab2 DG |
728 | |
729 | /* This registry is only used by UST. */ | |
730 | call_rcu(®p->node.head, rcu_free_buffer_reg_pid); | |
731 | } | |
732 | ||
733 | /* | |
734 | * Destroy per PID and UID registry hash table. | |
735 | */ | |
736 | void buffer_reg_destroy_registries(void) | |
737 | { | |
738 | DBG3("Buffer registry destroy all registry"); | |
3c339053 FD |
739 | lttng_ht_destroy(buffer_registry_uid); |
740 | lttng_ht_destroy(buffer_registry_pid); | |
7972aab2 | 741 | } |