Fix: resource leak in enable-event command
[lttng-tools.git] / src / bin / lttng-sessiond / buffer-registry.c
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 _GNU_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 {
110 int ret = 0;
111 struct buffer_reg_uid *reg = NULL;
112
113 assert(regp);
114
115 reg = zmalloc(sizeof(*reg));
116 if (!reg) {
117 PERROR("zmalloc buffer registry uid");
118 ret = -ENOMEM;
119 goto error;
120 }
121
122 reg->registry = zmalloc(sizeof(struct buffer_reg_session));
123 if (!reg->registry) {
124 PERROR("zmalloc buffer registry uid session");
125 ret = -ENOMEM;
126 goto error;
127 }
128
129 reg->session_id = session_id;
130 reg->bits_per_long = bits_per_long;
131 reg->uid = uid;
132 reg->domain = domain;
133
134 reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
135 if (!reg->registry->channels) {
136 ret = -ENOMEM;
137 goto error_session;
138 }
139
140 cds_lfht_node_init(&reg->node.node);
141 *regp = reg;
142
143 DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d",
144 session_id, bits_per_long, uid, domain);
145
146 return 0;
147
148 error_session:
149 free(reg->registry);
150 error:
151 free(reg);
152 return ret;
153 }
154
155 /*
156 * Add a buffer registry per UID object to the global registry.
157 */
158 void buffer_reg_uid_add(struct buffer_reg_uid *reg)
159 {
160 struct cds_lfht_node *nodep;
161 struct lttng_ht *ht = buffer_registry_uid;
162
163 assert(reg);
164
165 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64 ,
166 reg->session_id);
167
168 rcu_read_lock();
169 nodep = cds_lfht_add_unique(ht->ht, ht->hash_fct(reg, lttng_ht_seed),
170 ht->match_fct, reg, &reg->node.node);
171 assert(nodep == &reg->node.node);
172 rcu_read_unlock();
173 }
174
175 /*
176 * Find a buffer registry per UID object with given params. RCU read side lock
177 * MUST be acquired before calling this and hold on to protect the object.
178 *
179 * Return the object pointer or NULL on error.
180 */
181 struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id,
182 uint32_t bits_per_long, uid_t uid)
183 {
184 struct lttng_ht_node_u64 *node;
185 struct lttng_ht_iter iter;
186 struct buffer_reg_uid *reg = NULL, key;
187 struct lttng_ht *ht = buffer_registry_uid;
188
189 /* Setup key we are looking for. */
190 key.session_id = session_id;
191 key.bits_per_long = bits_per_long;
192 key.uid = uid;
193
194 DBG3("Buffer registry per UID find id: %" PRIu64 ", ABI: %u, uid: %d",
195 session_id, bits_per_long, uid);
196
197 /* Custom lookup function since it's a different key. */
198 cds_lfht_lookup(ht->ht, ht->hash_fct(&key, lttng_ht_seed), ht->match_fct,
199 &key, &iter.iter);
200 node = lttng_ht_iter_get_node_u64(&iter);
201 if (!node) {
202 goto end;
203 }
204 reg = caa_container_of(node, struct buffer_reg_uid, node);
205
206 end:
207 return reg;
208 }
209
210 /*
211 * Initialize global buffer per PID registry. Should only be called ONCE!.
212 */
213 void buffer_reg_init_pid_registry(void)
214 {
215 /* Should be called once. */
216 assert(!buffer_registry_pid);
217 buffer_registry_pid = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
218 assert(buffer_registry_pid);
219
220 DBG3("Global buffer per PID registry initialized");
221 }
222
223 /*
224 * Allocate and initialize object. Set regp with the object pointer.
225 *
226 * Return 0 on success else a negative value and regp is untouched.
227 */
228 int buffer_reg_pid_create(uint64_t session_id, struct buffer_reg_pid **regp)
229 {
230 int ret = 0;
231 struct buffer_reg_pid *reg = NULL;
232
233 assert(regp);
234
235 reg = zmalloc(sizeof(*reg));
236 if (!reg) {
237 PERROR("zmalloc buffer registry pid");
238 ret = -ENOMEM;
239 goto error;
240 }
241
242 reg->registry = zmalloc(sizeof(struct buffer_reg_session));
243 if (!reg->registry) {
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;
251
252 reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
253 if (!reg->registry->channels) {
254 ret = -ENOMEM;
255 goto error_session;
256 }
257
258 lttng_ht_node_init_u64(&reg->node, reg->session_id);
259 *regp = reg;
260
261 DBG3("Buffer registry per PID created with session id: %" PRIu64,
262 session_id);
263
264 return 0;
265
266 error_session:
267 free(reg->registry);
268 error:
269 free(reg);
270 return ret;
271 }
272
273 /*
274 * Add a buffer registry per PID object to the global registry.
275 */
276 void buffer_reg_pid_add(struct buffer_reg_pid *reg)
277 {
278 assert(reg);
279
280 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64,
281 reg->session_id);
282
283 rcu_read_lock();
284 lttng_ht_add_unique_u64(buffer_registry_pid, &reg->node);
285 rcu_read_unlock();
286 }
287
288 /*
289 * Find a buffer registry per PID object with given params. RCU read side lock
290 * MUST be acquired before calling this and hold on to protect the object.
291 *
292 * Return the object pointer or NULL on error.
293 */
294 struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id)
295 {
296 struct lttng_ht_node_u64 *node;
297 struct lttng_ht_iter iter;
298 struct buffer_reg_pid *reg = NULL;
299 struct lttng_ht *ht = buffer_registry_pid;
300
301 DBG3("Buffer registry per PID find id: %" PRIu64, session_id);
302
303 lttng_ht_lookup(ht, &session_id, &iter);
304 node = lttng_ht_iter_get_node_u64(&iter);
305 if (!node) {
306 goto end;
307 }
308 reg = caa_container_of(node, struct buffer_reg_pid, node);
309
310 end:
311 return reg;
312 }
313
314 /*
315 * Allocate and initialize a buffer registry channel with the given key. Set
316 * regp with the object pointer.
317 *
318 * Return 0 on success or else a negative value keeping regp untouched.
319 */
320 int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp)
321 {
322 struct buffer_reg_channel *reg;
323
324 assert(regp);
325
326 DBG3("Buffer registry channel create with key: %" PRIu64, key);
327
328 reg = zmalloc(sizeof(*reg));
329 if (!reg) {
330 PERROR("zmalloc buffer registry channel");
331 return -ENOMEM;
332 }
333
334 reg->key = key;
335 CDS_INIT_LIST_HEAD(&reg->streams);
336 pthread_mutex_init(&reg->stream_list_lock, NULL);
337
338 lttng_ht_node_init_u64(&reg->node, key);
339 *regp = reg;
340
341 return 0;
342 }
343
344 /*
345 * Allocate and initialize a buffer registry stream. Set regp with the object
346 * pointer.
347 *
348 * Return 0 on success or else a negative value keeping regp untouched.
349 */
350 int buffer_reg_stream_create(struct buffer_reg_stream **regp)
351 {
352 struct buffer_reg_stream *reg;
353
354 assert(regp);
355
356 DBG3("Buffer registry creating stream");
357
358 reg = zmalloc(sizeof(*reg));
359 if (!reg) {
360 PERROR("zmalloc buffer registry stream");
361 return -ENOMEM;
362 }
363
364 *regp = reg;
365
366 return 0;
367 }
368
369 /*
370 * Add stream to the list in the channel.
371 */
372 void buffer_reg_stream_add(struct buffer_reg_stream *stream,
373 struct buffer_reg_channel *channel)
374 {
375 assert(stream);
376 assert(channel);
377
378 pthread_mutex_lock(&channel->stream_list_lock);
379 cds_list_add_tail(&stream->lnode, &channel->streams);
380 channel->stream_count++;
381 pthread_mutex_unlock(&channel->stream_list_lock);
382 }
383
384 /*
385 * Add a buffer registry channel object to the given session.
386 */
387 void buffer_reg_channel_add(struct buffer_reg_session *session,
388 struct buffer_reg_channel *channel)
389 {
390 assert(session);
391 assert(channel);
392
393 rcu_read_lock();
394 lttng_ht_add_unique_u64(session->channels, &channel->node);
395 rcu_read_unlock();
396 }
397
398 /*
399 * Find a buffer registry channel object with the given key. RCU read side lock
400 * MUST be acquired and hold on until the object reference is not needed
401 * anymore.
402 *
403 * Return the object pointer or NULL on error.
404 */
405 struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key,
406 struct buffer_reg_uid *reg)
407 {
408 struct lttng_ht_node_u64 *node;
409 struct lttng_ht_iter iter;
410 struct buffer_reg_channel *chan = NULL;
411 struct lttng_ht *ht;
412
413 assert(reg);
414
415 switch (reg->domain) {
416 case LTTNG_DOMAIN_UST:
417 ht = reg->registry->channels;
418 break;
419 default:
420 assert(0);
421 goto end;
422 }
423
424 lttng_ht_lookup(ht, &key, &iter);
425 node = lttng_ht_iter_get_node_u64(&iter);
426 if (!node) {
427 goto end;
428 }
429 chan = caa_container_of(node, struct buffer_reg_channel, node);
430
431 end:
432 return chan;
433 }
434
435 /*
436 * Destroy a buffer registry stream with the given domain.
437 */
438 void buffer_reg_stream_destroy(struct buffer_reg_stream *regp,
439 enum lttng_domain_type domain)
440 {
441 if (!regp) {
442 return;
443 }
444
445 DBG3("Buffer registry stream destroy with handle %d",
446 regp->obj.ust->handle);
447
448 switch (domain) {
449 case LTTNG_DOMAIN_UST:
450 {
451 int ret;
452
453 ret = ust_ctl_release_object(-1, regp->obj.ust);
454 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
455 ERR("Buffer reg stream release obj handle %d failed with ret %d",
456 regp->obj.ust->handle, ret);
457 }
458 free(regp->obj.ust);
459 lttng_fd_put(LTTNG_FD_APPS, 2);
460 break;
461 }
462 default:
463 assert(0);
464 }
465
466 free(regp);
467 return;
468 }
469
470 /*
471 * Remove buffer registry channel object from the session hash table. RCU read
472 * side lock MUST be acquired before calling this.
473 */
474 void buffer_reg_channel_remove(struct buffer_reg_session *session,
475 struct buffer_reg_channel *regp)
476 {
477 int ret;
478 struct lttng_ht_iter iter;
479
480 assert(session);
481 assert(regp);
482
483 iter.iter.node = &regp->node.node;
484 ret = lttng_ht_del(session->channels, &iter);
485 assert(!ret);
486 }
487
488 /*
489 * Destroy a buffer registry channel with the given domain.
490 */
491 void buffer_reg_channel_destroy(struct buffer_reg_channel *regp,
492 enum lttng_domain_type domain)
493 {
494 if (!regp) {
495 return;
496 }
497
498 DBG3("Buffer registry channel destroy with key %" PRIu32, regp->key);
499
500 switch (domain) {
501 case LTTNG_DOMAIN_UST:
502 {
503 int ret;
504 struct buffer_reg_stream *sreg, *stmp;
505 /* Wipe stream */
506 cds_list_for_each_entry_safe(sreg, stmp, &regp->streams, lnode) {
507 cds_list_del(&sreg->lnode);
508 regp->stream_count--;
509 buffer_reg_stream_destroy(sreg, domain);
510 }
511
512 if (regp->obj.ust) {
513 ret = ust_ctl_release_object(-1, regp->obj.ust);
514 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
515 ERR("Buffer reg channel release obj handle %d failed with ret %d",
516 regp->obj.ust->handle, ret);
517 }
518 free(regp->obj.ust);
519 }
520 lttng_fd_put(LTTNG_FD_APPS, 1);
521 break;
522 }
523 default:
524 assert(0);
525 }
526
527 free(regp);
528 return;
529 }
530
531 /*
532 * Destroy a buffer registry session with the given domain.
533 *
534 * Should *NOT* be called with RCU read-side lock held.
535 */
536 static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
537 enum lttng_domain_type domain)
538 {
539 int ret;
540 struct lttng_ht_iter iter;
541 struct buffer_reg_channel *reg_chan;
542
543 DBG3("Buffer registry session destroy");
544
545 /* Destroy all channels. */
546 rcu_read_lock();
547 cds_lfht_for_each_entry(regp->channels->ht, &iter.iter, reg_chan,
548 node.node) {
549 ret = lttng_ht_del(regp->channels, &iter);
550 assert(!ret);
551 buffer_reg_channel_destroy(reg_chan, domain);
552 }
553 rcu_read_unlock();
554
555 ht_cleanup_push(regp->channels);
556
557 switch (domain) {
558 case LTTNG_DOMAIN_UST:
559 ust_registry_session_destroy(regp->reg.ust);
560 free(regp->reg.ust);
561 break;
562 default:
563 assert(0);
564 }
565
566 free(regp);
567 return;
568 }
569
570 /*
571 * Remove buffer registry UID object from the global hash table.
572 */
573 void buffer_reg_uid_remove(struct buffer_reg_uid *regp)
574 {
575 int ret;
576 struct lttng_ht_iter iter;
577
578 assert(regp);
579
580 rcu_read_lock();
581 iter.iter.node = &regp->node.node;
582 ret = lttng_ht_del(buffer_registry_uid, &iter);
583 assert(!ret);
584 rcu_read_unlock();
585 }
586
587 static void rcu_free_buffer_reg_uid(struct rcu_head *head)
588 {
589 struct lttng_ht_node_u64 *node =
590 caa_container_of(head, struct lttng_ht_node_u64, head);
591 struct buffer_reg_uid *reg =
592 caa_container_of(node, struct buffer_reg_uid, node);
593
594 buffer_reg_session_destroy(reg->registry, reg->domain);
595 free(reg);
596 }
597
598 static void rcu_free_buffer_reg_pid(struct rcu_head *head)
599 {
600 struct lttng_ht_node_u64 *node =
601 caa_container_of(head, struct lttng_ht_node_u64, head);
602 struct buffer_reg_pid *reg =
603 caa_container_of(node, struct buffer_reg_pid, node);
604
605 buffer_reg_session_destroy(reg->registry, LTTNG_DOMAIN_UST);
606 free(reg);
607 }
608
609 /*
610 * Destroy buffer registry per UID. The given pointer is NOT removed from any
611 * list or hash table. Use buffer_reg_pid_remove() before calling this function
612 * for the case that the object is in the global hash table.
613 */
614 void buffer_reg_uid_destroy(struct buffer_reg_uid *regp,
615 struct consumer_output *consumer)
616 {
617 struct consumer_socket *socket;
618
619 if (!regp) {
620 return;
621 }
622
623 DBG3("Buffer registry per UID destroy with id: %" PRIu64 ", ABI: %u, uid: %d",
624 regp->session_id, regp->bits_per_long, regp->uid);
625
626 if (!consumer) {
627 goto destroy;
628 }
629
630 rcu_read_lock();
631 /* Get the right socket from the consumer object. */
632 socket = consumer_find_socket_by_bitness(regp->bits_per_long,
633 consumer);
634 if (!socket) {
635 goto unlock;
636 }
637
638 switch (regp->domain) {
639 case LTTNG_DOMAIN_UST:
640 if (regp->registry->reg.ust->metadata_key) {
641 /* Return value does not matter. This call will print errors. */
642 (void) consumer_close_metadata(socket,
643 regp->registry->reg.ust->metadata_key);
644 }
645 break;
646 default:
647 assert(0);
648 rcu_read_unlock();
649 return;
650 }
651
652 unlock:
653 rcu_read_unlock();
654 destroy:
655 call_rcu(&regp->node.head, rcu_free_buffer_reg_uid);
656 }
657
658 /*
659 * Remove buffer registry UID object from the global hash table. RCU read side
660 * lock MUST be acquired before calling this.
661 */
662 void buffer_reg_pid_remove(struct buffer_reg_pid *regp)
663 {
664 int ret;
665 struct lttng_ht_iter iter;
666
667 assert(regp);
668
669 iter.iter.node = &regp->node.node;
670 ret = lttng_ht_del(buffer_registry_pid, &iter);
671 assert(!ret);
672 }
673
674 /*
675 * Destroy buffer registry per PID. The pointer is NOT removed from the global
676 * hash table. Call buffer_reg_pid_remove() before that if the object was
677 * previously added to the global hash table.
678 */
679 void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
680 {
681 if (!regp) {
682 return;
683 }
684
685 DBG3("Buffer registry per PID destroy with id: %" PRIu64,
686 regp->session_id);
687
688 /* This registry is only used by UST. */
689 call_rcu(&regp->node.head, rcu_free_buffer_reg_pid);
690 }
691
692 /*
693 * Destroy per PID and UID registry hash table.
694 *
695 * Should *NOT* be called with RCU read-side lock held.
696 */
697 void buffer_reg_destroy_registries(void)
698 {
699 DBG3("Buffer registry destroy all registry");
700 ht_cleanup_push(buffer_registry_uid);
701 ht_cleanup_push(buffer_registry_pid);
702 }
This page took 0.044673 seconds and 4 git commands to generate.