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