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